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. Joomla 3.4.6 远程代码执行 漏洞复现

    使用利用工具 https://github.com/YangSirrr/Joomla-3.4.6-RCE http://192.168.43.230/Joomla//configuration.php ...

  2. learning java ATW ScrollPane

    import java.awt.*; public class ScrollPaneTest { public static void main(String[] args) { var f = ne ...

  3. am335x system upgrade kernel can(八)

    1      Scope of Document This document describes can bus hardware design and can bus driver developm ...

  4. Kindle Touch 修砖手札

    首先是网上的修砖教程: 最近有多人反映按照修砖程序走过后依然板砖,和碎平联系和WA沟通后对帖子作新的修改. 新教程直接使用5.1.2的镜像,特别说明. 特别感谢kn007的专业指导 小白帖子现为简化过 ...

  5. C/C++输入

    fgets(str,n,stdin) 从键盘输入一行,替代gets().读取到n-1字节时或换行符时终止,如果是文件的话,读到文件结尾也会停止 getline(cin,str) str的类型必须是st ...

  6. Atcoder Grand Contest 026 (AGC026) F - Manju Game 博弈,动态规划

    原文链接www.cnblogs.com/zhouzhendong/AGC026F.html 前言 太久没有发博客了,前来水一发. 题解 不妨设先手是 A,后手是 B.定义 \(i\) 为奇数时,\(a ...

  7. Iptables 之二扩展模块 nat

    问题一:如果开发被动模式的ftp服务? 21号端口是命令连接端口,数据连接端口不固定 三步骤: (1)装载ftp追踪时的专用的模块 /lib/modules/$(uname-r)/kernel/ker ...

  8. 使用sqlyog连接 Mysql 出现1251错误

    错误如图所示: 错误详情信息: client does not support authentication protocol requested by server;consider upgradi ...

  9. 12.linux上Apache虚拟主机的建立和https协议网站建立

    一.Apache虚拟主机的建立   虚拟web主机 在同一台服务器上建立多个web站点,每个站点不独占用一台真正的服务器       1.建立dns解析 两个域名同一个ip               ...

  10. Tomcat学习四步走:内核、集群、参数及性能

    主题简介: 内核实现原理 分布式集群 生产部署关键参数 性能监控和分析 一.内核实现原理 HTTP Web服务器与浏览器之间以HTTP协议通信,浏览器要访问服务器即向服务器发送HTTP请求报文. 如图 ...