[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 ...
随机推荐
- .NET httpClient Post请求,GET请求方法
1.后端是WebAPI,POST请求,修饰符是[FromBody]的字符串,[FromBody]修饰的时候数据是来自body部分,而不是来自url部分,所以后端取值会自动映射出数据,比如后端是这样的, ...
- 文献阅读报告 - Social LSTM:Human Trajectory Prediction in Crowded Spaces
概览 简述 文献所提出的模型旨在解决交通中行人的轨迹预测(pedestrian trajectory prediction)问题,特别是在拥挤环境中--人与人交互(interaction)行为常有发生 ...
- CodeForces 1000C Covered Points Count(区间线段覆盖问题,差分)
https://codeforces.com/problemset/problem/1000/C 题意: 有n个线段,覆盖[li,ri],最后依次输出覆盖层数为1~n的点的个数. 思路: 区间线段覆盖 ...
- POJ 3659 再谈树形DP
Cell Phone Network Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 5325 Accepted: 188 ...
- SQL基础教程(第2版)第3章 聚合与排序:3-2 对表进行分组
第3章 聚合与排序:3-2 对表进行分组 ● 使用GROUP BY子句可以像切蛋糕那样将表分割.通过使用聚合函数和GROUP BY子句,可以根据“商品种类”或者“登记日期”等将表分割后再进行汇总.● ...
- Nginx和php交互的两种方式
Unix socket 也叫IPC socket 也就是进程间通信套接字用于同一台主机上的不同进程间交换数据 TCP socket IP socket要利用主机的传输层(tcp),可以用于同一台主机 ...
- Maven--超级 POM
对于 Maven3,超级 POM 在文件 %MAVEN_HOME%/lib/maven-model-builder-x.x.x.jar 中的 org/apache/maven/model/pom-4. ...
- Python D9 学习
Python 设置环境 当安装好Python 后 在计算机的属性里面 高级语言设置 环境变量. 环境变量里面的path 更改为Python的 树目录 可以从计算机直接下达命令 打开Pytho ...
- 通过gitlab的webhook触发Jenkins自动构建设置
1.Jenkins job中勾选Build when a change is pushed to GitLab 2.Gitlab project 页面setting选择Integrations,配置w ...
- vue多选验证
vue select 多选 验证 <FormItem :prop="'formList.'+index+'.name'" label="姓名" :rule ...