[LintCode] 619 Binary Tree Longest Consecutive Sequence III 二叉树最长连续序列 III
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的更多相关文章
- [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 ...
- [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 ...
- [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 ...
- LeetCode Binary Tree Longest Consecutive Sequence
原题链接在这里:https://leetcode.com/problems/binary-tree-longest-consecutive-sequence/ 题目: Given a binary t ...
- [Locked] Binary Tree Longest Consecutive Sequence
Binary Tree Longest Consecutive Sequence Given a binary tree, find the length of the longest consecu ...
- LeetCode 549. Binary Tree Longest Consecutive Sequence II
原题链接在这里:https://leetcode.com/problems/binary-tree-longest-consecutive-sequence-ii/description/ 题目: G ...
- LeetCode 298. Binary Tree Longest Consecutive Sequence
原题链接在这里:https://leetcode.com/problems/binary-tree-longest-consecutive-sequence/ 题目: Given a binary t ...
- [LeetCode] Binary Tree Longest Consecutive Sequence 二叉树最长连续序列
Given a binary tree, find the length of the longest consecutive sequence path. The path refers to an ...
- Binary Tree Longest Consecutive Sequence
Given a binary tree, find the length of the longest consecutive sequence path (连续的路径,不是从小到大). The pa ...
随机推荐
- 文件操作2-Day3
一.文件操作流程 打开文件,得到文件句柄并赋值给一个变量 通过句柄对文件进行操作 关闭文件 二.文件打开模式 1.普通打开模式 r:只读模式(不加参数则默认只读:不能写,不能追加) w:只写模式(只能 ...
- YAML_03 用playbook安装Apache,修改端口,配置ServerName,修改主页,设置开机自启
ansible]# vim http.yml --- - hosts: cache remote_user: root tasks: - name: install one speci ...
- 常见的div布局
1.一列固定宽度且居中 <!DOCTYPE html> <html lang="en"> <head> <meta charset=&qu ...
- leetcode 712
这道题的思路:我是根据最长公共子序列的思路得来的. 最长公共子序列是: d[i][j]表示字符串s1前i个(0-i-1)字符,和字符串s2前j个(0-j-1)字符的最长公共子序列. 分情况讨论: 当s ...
- day25 内置常用模块(四): 模块和包
阅读目录: 模块 import from xxx import xxx 包 import from xxx import xxx from xxx import * __init__.p ...
- 本地spark下保存rdd为文件
写随笔大概也是做笔记记录下自己思考的意思吧,之前有些事情觉得做随笔还是比较有用的,mark一下一个有用的网址 关于rdd的操作,网上有很多很多的教程,当初全部顺一遍,除了对rdd这个类型有了点概念,剩 ...
- nodejs 日志框架winston笔记
winston是一款nodejs的日志库,本文以2.1.1版本为例,介绍一下使用方法. 1.基础用法 引用日志库,返回的是一个对象.包含一些构造器,实例方法. 其中transports是日志输出方式. ...
- ciscn2019华北赛区半决赛day1_web1题解
感谢buuoj的大佬们搭建的复现环境.作为一位CTF的初学者,我会把每个题目的writeup都写的尽量详细,希望能帮到后面的初学者. http://web42.buuoj.cn 文章会不定时继续完善, ...
- 三大框架 之 Hibernate生成策略与缓存策略(主键生成策略、持久化、持久化类划分、一级缓存、事物管理)
目录 Hibernate生成策略与缓存策略 主键生成策略 主键分类 主键的生成策略 持久化 什么是持久化 什么是持久化类 持久化类编写规则 持久化类的划分 三种状态区分 持久态对象特征 一级缓存 什么 ...
- html5的 embed元素 和 object元素
html5的 embed元素 和 object元素 一.总结 一句话总结: embed定义嵌入的内容,比如插件,比如flash object定义定义一个嵌入的对象,用于包含对象,比如图像.音频.视频. ...