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 二叉树最长连续序列的更多相关文章

  1. [LeetCode] Binary Tree Longest Consecutive Sequence 二叉树最长连续序列

    Given a binary tree, find the length of the longest consecutive sequence path. The path refers to an ...

  2. LeetCode 298. Binary Tree Longest Consecutive Sequence

    原题链接在这里:https://leetcode.com/problems/binary-tree-longest-consecutive-sequence/ 题目: Given a binary t ...

  3. [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 ...

  4. [LeetCode] 128. Longest Consecutive Sequence 求最长连续序列

    Given an unsorted array of integers, find the length of the longest consecutive elements sequence. F ...

  5. LeetCode 549. Binary Tree Longest Consecutive Sequence II

    原题链接在这里:https://leetcode.com/problems/binary-tree-longest-consecutive-sequence-ii/description/ 题目: G ...

  6. [LeetCode] Longest Consecutive Sequence 求最长连续序列

    Given an unsorted array of integers, find the length of the longest consecutive elements sequence. F ...

  7. 298. Binary Tree Longest Consecutive Sequence

    题目: Given a binary tree, find the length of the longest consecutive sequence path. The path refers t ...

  8. [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 ...

  9. 298. Binary Tree Longest Consecutive Sequence最长连续序列

    [抄题]: Given a binary tree, find the length of the longest consecutive sequence path. The path refers ...

随机推荐

  1. 管理员权限运行-C#程序

    C#程序以管理员权限运行 在Vista 和 Windows 7 及更新版本的操作系统,增加了 UAC(用户账户控制) 的安全机制,如果 UAC 被打开,用户即使以管理员权限登录,其应用程序默认情况下也 ...

  2. eclipse正常启动,debug无法启动,解决办法

  3. 用wpjam插件的朋友记得勾选移除工具栏

    今天ytkah在调试页面的时候发现网页一直出现32px高度的空白,非常奇怪,样式如下,全盘查找了关键词也没找到对应的样式文件,后面想到wpjam插件好像有个屏蔽选项,到那边设置一下说不定可以 < ...

  4. 微信小程序——音频播放器

    先来个效果图韵下味: 需求: 音频的播放,暂停,中间按钮状态的变化,播放时实时更新播放进度: 前进15s,后退15s: 进度条拖动. 一开始想着这3个功能应该挺简单的.不就是播放,暂停,前进,后退么~ ...

  5. POJ P2251 Dungeon Master 题解

    深搜,只不过是三维的. #include<iostream> #include<cstring> #include<cstdio> #include<algo ...

  6. 【洛谷P4319】 变化的道路 线段树分治+LCT

    最近学了一下线段树分治,感觉还蛮好用... 如果正常动态维护最大生成树的话用 LCT 就行,但是这里还有时间这一维的限制. 所以,我们就把每条边放到以时间为轴的线段树的节点上,然后写一个可撤销 LCT ...

  7. 机器学习---逻辑回归(二)(Machine Learning Logistic Regression II)

    在<机器学习---逻辑回归(一)(Machine Learning Logistic Regression I)>一文中,我们讨论了如何用逻辑回归解决二分类问题以及逻辑回归算法的本质.现在 ...

  8. 洛谷P1902 刺杀大使

    题目 二分加广搜 #include <bits/stdc++.h> using namespace std; int n, m, l, r, p[1001][1001], vis[1001 ...

  9. C博客作业--我的第一篇博客作业

    1你对网络专业或计算机专业了解是怎样的 由于从小就与电脑打交道,对于各类软件的生产非常感兴趣,所以在高三开学查询有什么专业的时候,就打算报与计算机有关的专业.我对计算机专业感到非常神奇,毕竟只是看似简 ...

  10. 初始CSS3小知识【99%人不知道的小技巧】

    一.引入样式    1.行内样式表   <h1 style="color: red;font-size: 18px;">10-30</h1>     2.内 ...