Given a k-ary tree, find the length of the longest consecutive sequence path.

The path could be start and end at any node in the tree

Example
An example of test data: k-ary tree 5<6<7<>,5<>,8<>>,4<3<>,5<>,3<>>>, denote the following structure:
     5
   /   \
  6     4
 /|\   /|\
7 5 8 3 5 3
Return 5, // 3-4-5-6-7

解法:和Binary Tree Longest Consecutive Sequence II一样的做法。II只要check一下left和right。这题因为有多个子节点,所以要用一个循环来check所有。

Java:

 class ResultType {
int globalMax;
int maxUp;
int maxDown;
public ResultType(int globalMax, int maxUp, int maxDown) {
this.globalMax = globalMax;
this.maxUp = maxUp;
this.maxDown = maxDown;
}
} public int longestConsecutive3(MultiTreeNode root) {
return helper(root).globalMax;
} public ResultType helper(MultiTreeNode root) { if (root == null) {
return new ResultType(0, 0, 0);
} int maxUp = 0;
int maxDown = 0;
int max = Integer.MIN_VALUE; for (MultiTreeNode child : root.children) { if (child == null) {
continue;
} ResultType childResult = helper(child);
if (child.val + 1 == root.val) {
maxDown = Math.max(maxDown, childResult.maxDown + 1);
} if (child.val - 1 == root.val) {
maxUp = Math.max(maxUp, childResult.maxUp + 1);
} max = Math.max(Math.max(max, childResult.globalMax), maxUp + maxDown + 1);
} return new ResultType(max, maxUp, maxDown);
}

  

类似题目:

[LeetCode] 298. Binary Tree Longest Consecutive Sequence 二叉树最长连续序列

[LeetCode] 549. Binary Tree Longest Consecutive Sequence II 二叉树最长连续序列之 II

[LintCode] 619 Binary Tree Longest Consecutive Sequence III 二叉树最长连续序列 III的更多相关文章

  1. [LeetCode] 549. Binary Tree Longest Consecutive Sequence II 二叉树最长连续序列之 II

    Given a binary tree, you need to find the length of Longest Consecutive Path in Binary Tree. Especia ...

  2. [LeetCode] Binary Tree Longest Consecutive Sequence II 二叉树最长连续序列之二

    Given a binary tree, you need to find the length of Longest Consecutive Path in Binary Tree. Especia ...

  3. [LeetCode] 298. Binary Tree Longest Consecutive Sequence 二叉树最长连续序列

    Given a binary tree, find the length of the longest consecutive sequence path. The path refers to an ...

  4. LeetCode Binary Tree Longest Consecutive Sequence

    原题链接在这里:https://leetcode.com/problems/binary-tree-longest-consecutive-sequence/ 题目: Given a binary t ...

  5. [Locked] Binary Tree Longest Consecutive Sequence

    Binary Tree Longest Consecutive Sequence Given a binary tree, find the length of the longest consecu ...

  6. LeetCode 549. Binary Tree Longest Consecutive Sequence II

    原题链接在这里:https://leetcode.com/problems/binary-tree-longest-consecutive-sequence-ii/description/ 题目: G ...

  7. LeetCode 298. Binary Tree Longest Consecutive Sequence

    原题链接在这里:https://leetcode.com/problems/binary-tree-longest-consecutive-sequence/ 题目: Given a binary t ...

  8. [LeetCode] Binary Tree Longest Consecutive Sequence 二叉树最长连续序列

    Given a binary tree, find the length of the longest consecutive sequence path. The path refers to an ...

  9. Binary Tree Longest Consecutive Sequence

    Given a binary tree, find the length of the longest consecutive sequence path (连续的路径,不是从小到大). The pa ...

随机推荐

  1. Oracle substr() 字符截取函数

    1.substr函数格式   (俗称:字符截取函数) 格式1: substr(string string, int a, int b); 格式2:substr(string string, int a ...

  2. ps -p {pid} -o etime获取进程运行时间是如何计算出来的?

    ps -p 986 -o etime可以获取进程986的执行时间,不论系统时间有没有发生改变,它都可以返回正确的结果: -bash-4.2$ ps -p 986 -o etime ELAPSED 13 ...

  3. windbg调试托管代码 .Net clr

    现在很多的程序都是多语言混合编程的,比如我司的产品,就是用C++/.net clr混合编制的.那么当我们调试这样的程序时,一定要注意,比如有时我们只看到c++的栈和名称,而.net clr的代码确看不 ...

  4. 关于Pi

  5. fedora安装设置

    添加视频解码rpmfusion源: sudo rpm -ivh http://download1.rpmfusion.org/free/fedora/rpmfusion-free-release-st ...

  6. shell脚本编程基础之文件测试

    文件测试判断条件 -e FILE:测试文件是否存在 -f FILE:测试文件是否为普通文件 -d FILE:测试指定路径是否为目录 -r FILE:测试当前用户对指定文件是否有读权限 -w FILE: ...

  7. package.json 版本号解释

    经常看到package.json中的各种版本号记录 比如 ~ ^ 等.其实是有个规范的.其遵循 semver. 具体的网站为:  http://semver.org/lang/zh-CN/

  8. 原创:从海量数据中查找出前k个最小或最大值的算法(java)

    现在有这么一道题目:要求从多个的数据中查找出前K个最小或最大值 分析:有多种方案可以实现.一.最容易想到的是先对数据快速排序,然后输出前k个数字.   二.先定义容量为k的数组,从源数据中取出前k个填 ...

  9. 常用命令备忘 lsof

    lsof命令 可以列出被进程所打开的文件的信息.被打开的文件可以是 1.普通的文件, 2.目录 3.网络文件系统的文件, 4.字符设备文件 5.(函数)共享库 6.管道,命名管道 7.符号链接 8.底 ...

  10. vue的ref试用

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8&quo ...