[LeetCode] 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
.
求二叉树的最长连续序列的长度,要从父节点到子节点。最长连续子序列必须是从root到leaf的方向。 比如 1->2,返回长度2, 比如1->3->4->5,返回3->4->5这个子序列的长度3。
解法:递归遍历binary tree,递归函数传入父节点的值,以帮助子节点判断是否连续。
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);
}
}
Python:
class Solution(object):
def longestConsecutive(self, root):
"""
:type root: TreeNode
:rtype: int
"""
self.max_len = 0 def longestConsecutiveHelper(root):
if not root:
return 0 left_len = longestConsecutiveHelper(root.left)
right_len = longestConsecutiveHelper(root.right) cur_len = 1
if root.left and root.left.val == root.val + 1:
cur_len = max(cur_len, left_len + 1);
if root.right and root.right.val == root.val + 1:
cur_len = max(cur_len, right_len + 1) self.max_len = max(self.max_len, cur_len, left_len, right_len) return cur_len longestConsecutiveHelper(root)
return self.max_len
C++:
class Solution {
public:
int longestConsecutive(TreeNode* root) {
if (!root) return 0;
int res = 0;
dfs(root, root->val, 0, res);
return res;
}
void dfs(TreeNode *root, int v, int out, int &res) {
if (!root) return;
if (root->val == v + 1) ++out;
else out = 1;
res = max(res, out);
dfs(root->left, root->val, out, res);
dfs(root->right, root->val, out, res);
}
};
C++:
class Solution {
public:
int longestConsecutive(TreeNode* root) {
if (!root) return 0;
int res = 0;
dfs(root, 1, res);
return res;
}
void dfs(TreeNode *root, int len, int &res) {
res = max(res, len);
if (root->left) {
if (root->left->val == root->val + 1) dfs(root->left, len + 1, res);
else dfs(root->left, 1, res);
}
if (root->right) {
if (root->right->val == root->val + 1) dfs(root->right, len + 1, res);
else dfs(root->right, 1, res);
}
}
};
C++:
class Solution {
public:
int longestConsecutive(TreeNode* root) {
return helper(root, NULL, 0);
}
int helper(TreeNode *root, TreeNode *p, int res) {
if (!root) return res;
res = (p && root->val == p->val + 1) ? res + 1 : 1;
return max(res, max(helper(root->left, root, res), helper(root->right, root, res)));
}
};
C++: 迭代
class Solution {
public:
int longestConsecutive(TreeNode* root) {
if (!root) return 0;
int res = 0;
queue<TreeNode*> q;
q.push(root);
while (!q.empty()) {
int len = 1;
TreeNode *t = q.front(); q.pop();
while ((t->left && t->left->val == t->val + 1) || (t->right && t->right->val == t->val + 1)) {
if (t->left && t->left->val == t->val + 1) {
if (t->right) q.push(t->right);
t = t->left;
} else if (t->right && t->right->val == t->val + 1) {
if (t->left) q.push(t->left);
t = t->right;
}
++len;
}
if (t->left) q.push(t->left);
if (t->right) q.push(t->right);
res = max(res, len);
}
return res;
}
};
类似题目:
[LeetCode] 128. Longest Consecutive Sequence 求最长连续序列
[LeetCode] 300. Longest Increasing Subsequence 最长递增子序列
[LeetCode] 549. Binary Tree Longest Consecutive Sequence II 二叉树最长连续序列 II
[LintCode] 619 Binary Tree Longest Consecutive Sequence III 二叉树最长连续序列 III
All LeetCode Questions List 题目汇总
[LeetCode] 298. Binary Tree Longest Consecutive Sequence 二叉树最长连续序列的更多相关文章
- [LeetCode] Binary Tree Longest Consecutive Sequence 二叉树最长连续序列
Given a binary tree, find the length of the longest consecutive sequence path. The path refers to an ...
- LeetCode 298. Binary Tree Longest Consecutive Sequence
原题链接在这里:https://leetcode.com/problems/binary-tree-longest-consecutive-sequence/ 题目: Given a binary 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 ...
- [LeetCode] 128. Longest Consecutive Sequence 求最长连续序列
Given an unsorted array of integers, find the length of the longest consecutive elements sequence. F ...
- LeetCode 549. Binary Tree Longest Consecutive Sequence II
原题链接在这里:https://leetcode.com/problems/binary-tree-longest-consecutive-sequence-ii/description/ 题目: G ...
- [LeetCode] Longest Consecutive Sequence 求最长连续序列
Given an unsorted array of integers, find the length of the longest consecutive elements sequence. F ...
- 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 ...
随机推荐
- ms08067 分析与利用
分析 漏洞位于 NetpwPathCanonicalize 函数里面,这个函数的作用在于处理路径中的 ..\ 和 .\ 信息.该函数声明如下: DWORD NetpwPathCanonicalize( ...
- 讲解Flume
Spark Streaming通过push模式和pull模式两种模式来集成Flume push模式:Spark Streaming端会启动一个基于Avro Socket Server的Receiver ...
- vue $emit、$on、$refs简介
1.$emit 触发当前实例上的事件.附加参数都会传给监听器回调 ex: 子组件调用父组件的方法并传递数据注意:子组件标签中的时间也不区分大小写要用“-”隔开 子组件: <template> ...
- react编写规范之组件组件的内容编写顺序
static 开头的类属性,如 defaultProps.propTypes. 构造函数,constructor. getter/setter(还不了解的同学可以暂时忽略). 组件生命周期. _ 开头 ...
- 多项式的各类计算(多项式的逆/开根/对数/exp/带余除法/多点求值)
预备知识:FFT/NTT 多项式的逆 给定一个多项式 F(x)F(x)F(x),请求出一个多项式 G(x)G(x)G(x),满足 F(x)∗G(x)≡1(mod xn)F(x)*G(x) \equiv ...
- go 学习 (五):goroutine 协程
一.goroutine 基础 定义 使用者分配足够多的任务,系统能自动帮助使用者把任务分配到 CPU 上,让这些任务尽量并发运作,此机制在Go中称作 goroutine goroutine 是 Go语 ...
- shell脚本 基础应用
变量分为普通变量可只读变量 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 ...
- 漏斗分析(Funnel Analysis)
什么是漏斗分析? 简单来讲,就是抽象出某个流程,观察流程中每一步的转化与流失. 漏斗的三个要素: 时间:特指漏斗的转化周期,即为完成每一层漏斗所需时间的集合 节点:每一层漏斗,就是一个节点 流量:就是 ...
- BootstrapTable 表格插件
BootStrap Table 下载:https://v3.bootcss.com/getting-started/ BootStrap Table Css:https://v3.bootcss.co ...
- centos6 启动docker报错
1.启动docker报错: # service docker stop Stopping docker: [ OK ] [root@RSING data2]# service docker start ...