LeetCode 298. Binary Tree Longest Consecutive Sequence
原题链接在这里:https://leetcode.com/problems/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.
题解:
DFS bottom-up方法.
Time Complexity: O(n). Space: O(logn).
AC Java:
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
int max = 0;
public int longestConsecutive(TreeNode root) {
dfs(root);
return max;
} private int dfs(TreeNode root){
if(root == null){
return 0;
} int l = dfs(root.left)+1;
int r = dfs(root.right)+1;
if(root.left != null && root.val != root.left.val-1){
l = 1;
} if(root.right != null && root.val != root.right.val-1){
r = 1;
} int curMax = Math.max(l, r);
max = Math.max(max, curMax);
return curMax;
}
}
也可采用top down的方法.
Time Complexity: O(n). Space: O(logn).
AC Java:
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public int longestConsecutive(TreeNode root) {
if(root == null){
return 0;
} int [] res = new int[]{0};
dfs(root, root.val, 1, res);
return res[0];
} private void dfs(TreeNode root, int target, int count, int [] res){
if(root == null){
return;
} if(root.val != target+1){
count = 1;
}else{
count++;
} res[0] = Math.max(res[0], count);
dfs(root.left, root.val, count, res);
dfs(root.right, root.val, count, res);
}
}
跟上Binary Tree Longest Consecutive Sequence II.
类似Longest Consecutive Sequence.
LeetCode 298. Binary Tree Longest Consecutive Sequence的更多相关文章
- [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] 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 549. Binary Tree Longest Consecutive Sequence II
原题链接在这里:https://leetcode.com/problems/binary-tree-longest-consecutive-sequence-ii/description/ 题目: G ...
- 298. Binary Tree Longest Consecutive Sequence
题目: Given a binary tree, find the length of the longest consecutive sequence path. The path refers t ...
- [LeetCode] 298. Binary Tree Longest Consecutive Sequence_Medium tag: DFS recursive
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 ...
- [LeetCode] 549. Binary Tree Longest Consecutive Sequence II_ Medium tag: DFS recursive
Given a binary tree, you need to find the length of Longest Consecutive Path in Binary Tree. Especia ...
- [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 ...
- [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 ...
随机推荐
- commonAncestor
commonAncestor 光标或选区所在区域最外层的祖先节点
- yii 2 局部关闭 CSRF 拦截
最近在拿 yii 2.0 开发ajax提交,在 post 请求接口时,提示数据无法验证,于是查询 yii 错误日志,发现错误为 exception ‘yiiwebBadRequestHttpExcep ...
- 每天一个Linux命令(46)ifconfig命令
在windows系统中,ipconfig命令行工具被用来获取网络接口配置信息并对此进行修改.Linux系统拥有一个类似的工具,也就是ifconfig(interfaces config). ( ...
- CentOS7在VMWare12中安装后不能上网解决办法
首先要保证你的VMWare Workstation12 在安装号CentOS7后没改动什么关于网络相关的. 1.我的电脑一开始用的是VMWare WorkStations10,发现VMnet8根本不通 ...
- Android相机实时自动对焦的完美实现
https://zhidao.baidu.com/question/873328177698804372.html Android相机实时自动对焦的完美实现 http://blog.csdn.net/ ...
- Struts2笔记04——Hello World Example(转)
原文地址:https://www.tutorialspoint.com/struts_2/ [注释]项目结构,次序估计有误 通过学习Struts2的架构,我们可以知道,在Struts2 web应用中, ...
- 在安装mysqli的时候,出现error: ext/mysqlnd/mysql_float_to_double.h: No such file or directory
/application/php5.:: warning: /ext/mysqli/mysqli_api.c::: error: ext/mysqlnd/mysql_float_to_double.h ...
- MySQL数据库基本操作(一)
进入mysql 本地连接: mysql -u用户名 -p 输入密码 qwe123 mysql -uroot -pqwe123 sudo apt-get install mysql-server # p ...
- 如何用hexo+github搭建个人博客
搭建环境 1.安装 Node.js: https://nodejs.org/en/ windows下点击链接,下载安装即可;Linux下更加简单,在终端下输入sudo apt-get install ...
- Android 下的usb框架及功能点【转】
本文转载自:https://blog.csdn.net/tianruxishui/article/details/37902959 有关USB android框架的链接 http://blog.sin ...