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. 12.24笔记(关于//UIDynamic演练//多对象的附加行为//UIDynamic简单演练//UIDynamic//(CoreText框架)NSAttributedString)

          12.24笔记1.UIDynamic注意点:演示代码:上面中设置视图旋转的时候,需要注意设置M_PI_4时,视图两边保持平衡状态,达不到仿真效果.需要偏移下角度.2.吸附行为3.推动行为初 ...

  2. 【洛谷 T47488】 D:希望 (点分治)

    题目链接 看到这种找树链的题目肯定是想到点分治的. 我码了一下午,\(debug\)一晚上,终于做到只有两个点TLE了. 我的是不完美做法 加上特判\(A\)了这题qwq 记录每个字母在母串中出现的所 ...

  3. HTML5获取地理位置信息并在Google Maps上显示

    <!DOCTYPE HTML> <html lang="en-US"> <head> <meta charset="UTF-8& ...

  4. c语言中网络字节序和主机字节序的转换

    函数说明   相关函数:htonl, htons, ntohl 头文件:#include <netinet/in.h> 定义函数:unsigned short int ntohs(unsi ...

  5. 20179205《Linux内核原理与分析》第一周作业

    输出 shiyanlou 图形字符的命令banner: 新建用户wangyazhe,输入密码不会显示出来: 利用sudo adduser添加一个用户 loutest,mkdir创建一个新的文件夹opt ...

  6. inetdev_init && inetdev_destroy

    inetdev_init为传入设备分配和绑定ip控制块,查看其调用关系如下: fs_initcall(inet_init)---->inet_init---->ip_init----> ...

  7. go语言爬虫goquery和grequests的使用

    /*下载工具*/ package main import ( "fmt" //go语言版本的jquery "github.com/PuerkitoBio/goquery& ...

  8. BZOJ 2002: [Hnoi2010]Bounce 弹飞绵羊 动态树

    题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=2002 题意:加边,删边,查询到根的距离. #include <bits/stdc++ ...

  9. JS常用操作方法

    1.splice() 方法向/从数组中添加/删除项目,然后返回被删除的项目. 注释:该方法会改变原始数组. 1 <script type="text/javascript"& ...

  10. English——Unit 1

    meditate  v.沉思,冥想:考虑,谋划 medtiation   n.沉思,冥想:深思熟虑 medium elaborate   adj.精心制作的,详尽的,复杂的:v.精心制作:详述(计划, ...