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.

分析:https://segmentfault.com/a/1190000003957798

递归法

复杂度

时间O(n) 空间O(h)

思路

因为要找最长的连续路径,我们在遍历树的时候需要两个信息,一是目前连起来的路径有多长,二是目前路径的上一个节点的值。我们通过递归把这些信息代入,然后通过返回值返回一个最大的就行了。这种需要遍历二叉树,然后又需要之前信息的题目思路都差不多,比如Maximum Depth of Binary TreeBinary Tree Maximum Path Sum

 public class Solution {
public int longestConsecutive(TreeNode root) {
if(root == null){
return ;
}
return findLongest(root, , root.val - );
} private int findLongest(TreeNode root, int length, int preVal){
if(root == null){
return length;
}
// 判断当前是否连续
int currLen = preVal + == root.val ? length + : ;
// 返回当前长度,左子树长度,和右子树长度中较大的那个
return Math.max(currLen, Math.max(findLongest(root.left, currLen, root.val), findLongest(root.right, currLen, root.val)));
}
}

另一种解法:http://www.cnblogs.com/grandyang/p/5252599.html

这道题让我们求二叉树的最长连续序列,关于二叉树的题基本都需要遍历树,而递归遍历写起来特别简单,下面这种解法是用到了递归版的先序遍历,我们对于每个遍历到的节点,我们看节点值是否比参数值(父节点值)大1,如果是则长度加1,否则长度重置为1,然后更新结果res,再递归调用左右子节点即可,参见代码如下:

 class Solution {
public:
int longestConsecutive(TreeNode* root) {
if (!root) return ;
int res = ;
dfs(root, root->val - , , res);
return res;
}
void dfs(TreeNode *root, int v, int out, int &res) {
if (!root) return;
if (root->val == v + ) ++out;
else out = ;
res = max(res, out);
dfs(root->left, root->val, out, res);
dfs(root->right, root->val, out, res);
}
};

Binary Tree Longest Consecutive Sequence的更多相关文章

  1. LeetCode Binary Tree Longest Consecutive Sequence

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

  2. [Locked] Binary Tree Longest Consecutive Sequence

    Binary Tree Longest Consecutive Sequence Given a binary tree, find the length of the longest consecu ...

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

  4. LeetCode 549. Binary Tree Longest Consecutive Sequence II

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

  5. LeetCode 298. Binary Tree Longest Consecutive Sequence

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

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

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

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

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

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

随机推荐

  1. PetaPoco dynamic

    PetaPoco最初的灵感来自Massive-通过dynamic Expando objects返回一切.对于大多数情况我觉得这比较麻烦,更喜欢强类型的类.但是有些时候支持dynamic也是有用的-特 ...

  2. MYSQL双主故障解决实例。

    根据报错得知,获取到的主库文件格式错误. 一.锁住主从正常的库 Mysql> flush tables with read lock; 锁表 unlock tables; 解锁 SHOW MAS ...

  3. Xcode常用技巧(1)-使用Xcode进行代码分析及GDB调试

    1. 使用Xcode分析代码,分析分为静态分析及动态分析 静态分析:(Xcode-Product-Analyze)  检测代码是否有潜在的内存泄露  编译器认为不太合适的代码 运行结果: 若程序有 ...

  4. 【转】 详解Kafka生产者Producer配置

    粘贴一下这个配置,与我自己的程序做对比,看看能不能完善我的异步带代码:   -----------------------------------------    详解Kafka生产者Produce ...

  5. ExtJS -- Grid 文本居中显示

    Reference: http://stackoverflow.com/questions/6561120/extjs-4-apply-defaults-to-all-columns-in-a-gri ...

  6. ELK常见错误分析(转)

    ELK 常见错误处理   ELK 这里就不介绍了,如何安装请参考博客之前的文章.在这里感谢ttlsa团队,同时,我很荣幸能加入到ttlsa团队中,分享点滴,凉白开说发文章有红包,期待这篇群主能给多少红 ...

  7. HDU 2007

    /*杭电ACM ID:2007*/ #define _CRT_SECURE_NO_WARNINGS #include <stdio.h> int main() { int in1, in2 ...

  8. PHP基础 mysqli的事务处理

    1: <?php 2: // PHP 的mysqli的事务处理 3: //======================================================== 4: ...

  9. Bots(逆元,递推)

    H. Bots time limit per test 1.5 seconds memory limit per test 256 megabytes input standard input out ...

  10. Android在TextView中实现RichText风格

    参考: Android实战技巧:用TextView实现Rich Text---在同一个TextView中设置不同的字体风格 Demo: private SpannableStringBuilder c ...