298. Binary Tree Longest Consecutive Sequence
题目:
Given a binary tree, find the length of the longest consecutive sequence path.
The path refers to any sequence of nodes from some starting node to any node in the tree along the parent-child connections. The longest consecutive path need to be from parent to child (cannot be the reverse).
For example,
1
\
3
/ \
2 4
\
5
Longest consecutive sequence path is 3-4-5, so return 3.
2
\
3
/
2
/
1
Longest consecutive sequence path is 2-3,not3-2-1, so return 2.
链接: http://leetcode.com/problems/binary-tree-longest-consecutive-sequence/
题解:
求二叉树中最长连续序列。 题目又比较长, 不过我们可以确定这个最长连续序列肯定是递增的,比如123,或者345。知道这点以后就可以用DFS遍历了。
Time Complexity - O(n), Space Complexity - O(n)。
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
private int max = 0;
public int longestConsecutive(TreeNode root) {
if(root == null) {
return max;
}
findLongestConsecutive(root, 0, root.val);
return max;
} private void findLongestConsecutive(TreeNode root, int curMax, int target) {
if(root == null) {
return;
}
if(root.val == target) {
curMax++;
} else {
curMax = 1;
}
max = Math.max(max, curMax);
findLongestConsecutive(root.left, curMax, root.val + 1);
findLongestConsecutive(root.right, curMax, root.val + 1);
}
}
二刷:
根据题目的意思,最长连续子序列必须是从root到leaf的方向。 比如 1->2,那么我们就返回长度2, 比如1->3->4->5,我们就返回3->4->5这个子序列的长度3。把树遍历一遍就可以得到结果了。
方法和一刷一样,构建一个辅助方法,形参是当前要处理的节点root,root父节点的值lastVal,以及当前的长度curLen。也要使用一个global variable maxLen来保存全局最大长度。也有使用stack来iterative遍历的做法。
Java:
Time Complexity - O(n), Space Complexity - O(n)。
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
private int maxLen = 0; public int longestConsecutive(TreeNode root) {
longestConsecutive(root, 0, 0);
return maxLen;
} private void longestConsecutive(TreeNode root, int lastVal, int curLen) {
if (root == null) return;
if (root.val != lastVal + 1) curLen = 1;
else curLen++;
maxLen = Math.max(maxLen, curLen);
longestConsecutive(root.left, root.val, curLen);
longestConsecutive(root.right, root.val, curLen);
}
}

Reference:
https://leetcode.com/discuss/68723/simple-recursive-dfs-without-global-variable
https://leetcode.com/discuss/66486/c-solution-in-4-lines
https://leetcode.com/discuss/66565/1ms-easy-understand-java-solution-just-traverse-the-tree-once
https://leetcode.com/discuss/68094/dont-understand-what-is-consecutive-sequence
https://leetcode.com/discuss/66646/two-simple-iterative-solutions-bfs-and-dfs
https://leetcode.com/discuss/66548/recursive-solution-bottom-iteration-solution-using-stack
https://leetcode.com/discuss/66584/easy-java-dfs-is-there-better-time-complexity-solution
298. Binary Tree Longest Consecutive Sequence的更多相关文章
- LeetCode 298. Binary Tree Longest Consecutive Sequence
原题链接在这里:https://leetcode.com/problems/binary-tree-longest-consecutive-sequence/ 题目: Given a binary t ...
- [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 ...
- 298. Binary Tree Longest Consecutive Sequence最长连续序列
[抄题]: Given a binary tree, find the length of the longest consecutive sequence path. The path refers ...
- [LC] 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] 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 ...
- [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 star ...
- 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] Binary Tree Longest Consecutive Sequence II 二叉树最长连续序列之二
Given a binary tree, you need to find the length of Longest Consecutive Path in Binary Tree. Especia ...
随机推荐
- powerdesigner 技巧
1.修改建表脚本生成规则.如果每个表格都有相同的字段,可以如下修改: Database -> Edit Current DBMS 展开 Script -> Object -> Tab ...
- React + Reflux
React + Reflux 渲染性能优化原理 作者:ManfredHu 链接:http://www.manfredhu.com/2016/11/08/23-reactRenderingPrinc ...
- cocos2dx中导演的职责有哪些?
1.一个游戏里面只有一个导演,因此采用了单例的设计模式,用getInstance方法来获得 2.游戏中导演负责openGL ES的初始化,场景的切换,游戏的暂停继续(相当于拍电影的ka),节点坐标与世 ...
- “我爱淘”冲刺阶段Scrum站立会议8
完成任务: 今天最大的成功就是解决了昨天的问题,可以将xml文件的内容解析出来显示到软件中. 计划任务: 可以通过webservice将数据库中的内容解析出来,通过查询可以得到想要的内容. 遇到问题: ...
- Eigen库实现简单的旋转、平移操作
本来课程要求用GUI界面来实现Eigen的旋转.平移操作的,但是接触GUI编程时间太短,虽然要求很简单,但是做了几天还是没有完成.就把命令行下面的简单的贴一下吧. main.cpp #include ...
- JavaScript DOM动态创建(声明)Object元素
http://www.cnblogs.com/GuominQiu/archive/2011/04/01/2002783.html 一文提及“等整个页面加载完毕后,根据用户所选的阅读机类型,再用Java ...
- 用setTimeout 代替 setInterval实时拉取数据
在开发中,我们常常碰到需要定时拉取网站数据,如: setInterval(function(){ $.ajax({ url: 'xx', success: function( response ){ ...
- sqlserver2008r2 127.0.0.1 用户sa登录失败 错误18456
按照网上的所有方法都试过了,还是不行. 最后,将sa密码重新设置一下,解决问题.
- Keil中的code关键字
一般说来,我们在C语言中定义的每一个变量初始化后都会占用一定的内存(RAM)空间.但是在keil中提供了一个特殊的关键字“code”,这个关键字在标准C中是没有的.其语法举例如下: unsigned ...
- ios 图片转视频
转自:http://blog.iosxcode4.com/archives/160 用到的FrameWork有: MediaPlayer.framework,QuartzCore.framework, ...