首页 > 其他分享 >Tree Longest Path 2246

Tree Longest Path 2246

时间:2022-10-31 11:57:21浏览次数:83  
标签:parent int tree Tree list length path Path 2246

2246. Longest Path With Different Adjacent Characters Hard

You are given a tree (i.e. a connected, undirected graph that has no cycles) rooted at node 0 consisting of n nodes numbered from 0 to n - 1. The tree is represented by a 0-indexed array parent of size n, where parent[i] is the parent of node i. Since node 0 is the root, parent[0] == -1.

You are also given a string s of length n, where s[i] is the character assigned to node i.

Return the length of the longest path in the tree such that no pair of adjacent nodes on the path have the same character assigned to them.

 

Example 1:

Input: parent = [-1,0,0,1,1,2], s = "abacbe"
Output: 3
Explanation: The longest path where each two adjacent nodes have different characters in the tree is the path: 0 -> 1 -> 3. The length of this path is 3, so 3 is returned.
It can be proven that there is no longer path that satisfies the conditions. 

Example 2:

Input: parent = [-1,0,0,0], s = "aabc"
Output: 3
Explanation: The longest path where each two adjacent nodes have different characters is the path: 2 -> 0 -> 3. The length of this path is 3, so 3 is returned.

 Constraints:

  • n == parent.length == s.length
  • 1 <= n <= 105
  • 0 <= parent[i] <= n - 1 for all i >= 1
  • parent[0] == -1
  • parent represents a valid tree.
  • s consists of only lowercase English letters.
class Solution {
    private int max = 1;
    public int longestPath(int[] parent, String s) {
        //build tree, from parent to child
        List<Integer>[] tree = new List[parent.length];
        for(int i = 0; i < parent.length; i++) {
            if(parent[i] == -1) continue;
            if(tree[parent[i]] == null) {
                tree[parent[i]] = new ArrayList();
            }
            tree[parent[i]].add(i);
        }
        //dfs
        dfs(tree, 0, s);
        //return result
        return max;
    }
    /**
    * input: tree, current node, string
    * return: curr node's single longest path
    */
    private int dfs(List<Integer>[] tree, int root, String s){
        if(tree[root] == null) return 1;
        int currmax = 0;
        //used to store all the result of each child
        List<Integer> list = new ArrayList();
        for(int child : tree[root]){
            int curr = dfs(tree, child, s);
            if(s.charAt(child) != s.charAt(root)) list.add(curr);
            else list.add(0);
        }
        Collections.sort(list, (x, y) -> y-x);
        max = Math.max(list.get(0) + (list.size() <= 1 ? 0 : list.get(1) ) + 1, max);
        return list.get(0) + 1;
    }
}

 相关题目后续补充: 

543.Diameter-of-Binary-Tree (M)
124.Binary-Tree-Maximum-Path-Sum (M)
687.Longest-Univalue-Path (M+)
1522.Diameter-of-N-Ary-Tree (M)
2049.Count-Nodes-With-the-Highest-Score (M+)

标签:parent,int,tree,Tree,list,length,path,Path,2246
From: https://www.cnblogs.com/cynrjy/p/16843794.html

相关文章

  • FOR XML PATH 函数用法
    https://www.cnblogs.com/yasuo2/p/6433697.html   一.FORXMLPATH简单介绍             那么还是首先来介绍一下FORXMLPATH,假设现在有一张兴趣爱......
  • UVA11297 Census(kd-tree)
    题意:给定一个\(n\timesn\)的网格,要求支持修改和询问某个矩阵的最大值和最小值。解法多样,可以用二维线段树,我用的是\(kd-tree\)。那么这题就很裸了,我在这里只提几点需......
  • Root的工程的访问,以及默认index.html页面的访问+Context,path,docBase
     当我们在浏览器地址栏中输入访问地址如下:http://ip:port/                 ====>>>>没有工程名的时候,默认访问的是 Root  工......
  • UI界面+treeviwe+dataGridView
    usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Data;usingSystem.Drawing;usingSystem.Linq;usingSystem.Runtime.Interop......
  • SourceTree的安装跳过注册账户
    今天安装sourcetree一直卡在注册界面,后来使用以下方法跳过注册步骤,亲测可用。1、地址栏直接输入%LocalAppData%\Atlassian(或者可以选择下载个EveryThing,搜索到目标文件夹......
  • LeetCode 2458. Height of Binary Tree After Subtree Removal Queries
    原题链接在这里:https://leetcode.com/problems/height-of-binary-tree-after-subtree-removal-queries/题目:Youaregiventhe root ofa binarytree with n node......
  • 【XSY3549】Tree(线段树,换根)
    原题不想说(太懒了),就说一下总结到的两点想法?对于树上多次询问路径信息的问题,如果两条路径的信息无法快速合并(即不能pushup),但是在路径两端增加/删除单点后的信息变化可以......
  • 【XSY3312】路径(path)(trick)
    原题就不说了,记录一下其中要用的一个trick。定理:对于一个\(1\simn\)的随机排列,它的前缀最大值的期望个数为\(O(\logn)\)。证明:考虑元素\(x\)作为前缀最大值的概......
  • 解决Vue项目: verbose stack Error: unable to resolve dependency tree
    解决Vue项目:verbosestackError:unabletoresolvedependencytree问题npminstall报错如上。解决......
  • go gopath配置
    cannotfindpackage"github.com/go-playground/validator/v10"inanyof:   /home/thk/local/go/src/vendor/github.com/go-playground/validator/v10(vendortree......