[LC] 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).
Example 1:
Input: 1
\
3
/ \
2 4
\
5 Output:3Explanation: Longest consecutive sequence path is3-4-5, so return3.
Example 2:
Input: 2
\
3
/
2
/
1 Output: 2 Explanation: Longest consecutive sequence path is2-3, not3-2-1, so return2.
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
int res = 0;
public int longestConsecutive(TreeNode root) {
if (root == null) {
return res;
}
helper(root, root.val, 0);
return res;
} private void helper(TreeNode root, int num, int cur) {
if (root == null) {
return;
}
if (root.val == num + 1) {
cur += 1;
} else {
cur = 1;
}
res = Math.max(res, cur);
helper(root.left, root.val, cur);
helper(root.right, root.val, cur);
}
}
[LC] 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 ...
- 298. Binary Tree Longest Consecutive Sequence
题目: Given a binary tree, find the length of the longest consecutive sequence path. The path refers t ...
- [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 ...
随机推荐
- js.console携程近期低价机票信息
!function(){var city = {"SHA":"上海虹桥","PVG":"上海浦东","YIW& ...
- Bugku 逆向
1.入门逆向 下载解压,在文件夹中打开命令行窗口执行一下:baby.exe 发现输出了一串字符,在将其放到IDA中然后是这样: 发现上面有一串输出和我们命令行窗口中的一样,但是下面为什么又多了一大溜东 ...
- i春秋-web- 爆破2
题目:flag不在变量中. 打开链接: <?php include "flag.php"; $a = @$_REQUEST['hello']; eval( "var ...
- pymysql常见报错
错误一: AttributeError: module 'pymysql' has no attribute 'connect' 有道翻译 AttributeError:模块'pymysql'没有属性 ...
- 2020/1/28 PHP代码审计之命令执行漏洞
0x00 命令执行漏洞原理 应用程序有时需要调用一些执行系统命令的函数,如在PHP中,使用system.exec.shell_exec.passthru.popen.proc_popen等函数可以执行 ...
- 小程序Java多次请求Session不变
微信小程序每次请求的sessionid是变化的,导致对应后台的session不一致,无法获取之前保存在session中的openid和sessionKey. 为了解决这个问题,需要强制同意每次小程序前 ...
- Java并发编程:CountDownLatch、CyclicBarrier和 Semaphore , Condition
http://www.importnew.com/21889.html 1)CountDownLatch和CyclicBarrier都能够实现线程之间的等待,只不过它们侧重点不同: CountDown ...
- 64)vertor 简单使用
1)简单 代码样例:我的理解 vector 其实就是一个简单的数组,然后通过迭代器来进行 遍历数组中的值,而且有自带push_back()来添加元素 #include<iostream&g ...
- tomcat启动极慢在linux生产环境
在window环境下不会出现启动极慢的问题. Tomcat启动极慢在生产环境,跟Weblogic一样 此外由于Weblogic创建域的时候使用的JDK是自带的jrockit,所以要解决WebL ...
- day62-html-标签
前端 blog链接:http://www.cnblogs.com/liwenzhou/p/7988087.html 1.前端都有哪些内容? HTML CSS JavaScript jQuery Boo ...