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. PHP流程控制之for循环控制语句

    王同学反复往返与北京和大连,并且在本上记录往返次数.在PHP中还有另外一种实现方式能够实现同样的计数.无锡大理石测量平台 for 循环是 PHP 中的一种计数型循环,它的语法比较数活多变.这是一个必须 ...

  2. SVN “Previous operation has not finished”

    https://jingyan.baidu.com/article/cbcede0761334902f40b4d31.html 需要运行sqlite3打开.svn下的wc.db数据库文件, sqlit ...

  3. LibreOJ #527. 「LibreOJ β Round #4」框架

    二次联通门 : LibreOJ #527. 「LibreOJ β Round #4」框架 /* LibreOJ #527. 「LibreOJ β Round #4」框架 %% xxy dalao 对于 ...

  4. Magma中ECC的点乘实例

    a:=-3;b:= 41058363725152142129326129780047268409114441015993725554835256314039467401291;E:= Elliptic ...

  5. luogu_P3313 [SDOI2014]旅行

    传送门 Solution 第二次学习可持久化线段树 打了一道裸题来练习一下-- 对于每个宗教都可以开一个主席树 基础操作 树剖lca Code  #include<bits/stdc++.h&g ...

  6. LCT 总结

    刚开始学lct花了一晚上研究模板,调出来就感觉不怎么难打了. 对板子的浅显理解: lct维护树形联通块,通过splay维护实链,可以把需要的路径变换到一颗splay上维护. splay中的关系只依赖实 ...

  7. useReducer代替Redux小案例-1(七)

    使用useContext和useReducer是可以实现类似Redux的效果,并且一些简单的个人项目,完全可以用下面的方案代替Redux,这种做法要比Redux简单一些.因为useContext和us ...

  8. [转]Myeclipse四种方式发布项目

    原文链接: myeclipse四种方式发布项目

  9. gogs 实现webhook钩子(php接口形式)

    1.概要流程 2.准备工作 gogs服务器 linux网站服务器(宝塔) 本地客户端 3.编写钩子访问的接口 在public下新建githook.php文件,代码如下: <?php $cmd = ...

  10. chrome 打开本地 pdf 会自动开启下载

    正解:修改注册表:[HKEY_CLASSES_ROOT\.pdf],将 Content Type 的值改为: application/pdf 即可