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. 【BZOJ】1014 [JSOI2008]火星人prefix

    [算法]splay [题解]对于每个结点维护其子树串的hash值,前面为高位,后面为低位. sum[x]=sum[L]*base[s[R]+1]+A[x]*base[s[R]]+sum[R],其中su ...

  2. 关于scala 集合 List Map Set

    1,数组 2,List,ListBuffer 3, Map , mutable.Map

  3. 获取天气api

    http://wthrcdn.etouch.cn/WeatherApi?citykey=101010100通过城市id获得天气数据,xml文件数据,当错误时会有<error>节点http: ...

  4. Ubuntu中启用关闭Network-manager网络设置问题! 【Server版本】

    在UbuntuServer版本中,因为只存有命令行模式,所以要想进行网络参数设置,只能通过修改/etc/network/interfaces.具体设置方法如下: (1) UbuntuServer 修改 ...

  5. linux编程之消息队列

    消息队列是内核地址空间中的内部链表,通过linux内核在各个进程之间传递内容,消息顺序地发送到消息队列中,并且以几种不同的方式 从队列中获取,每个消息队列可以用IPC标识符唯一的进行标识,内核中的消息 ...

  6. 【Educational Codeforces Round 19】

    这场edu蛮简单的…… 连道数据结构题都没有…… A.随便质因数分解凑一下即可. #include<bits/stdc++.h> #define N 100005 using namesp ...

  7. 文字顺时针旋转90度(纵向)&古诗词排版

    1.文字旋转90度 width: 100px; height: 200px; line-height: 100px; text-align: center; writing-mode: vertica ...

  8. 用JavaScript简单判断浏览器类型

    判断浏览器类型 /** * 判断浏览器类型 * 返回表示浏览器类型的字符串 */ function whoIsMe(){ var explorer = navigator.userAgent; if( ...

  9. 在Mysql中插入百万级别数据的方法

    首先,建立部门表和员工表: 部门表: create table dept( id int unsigned primary key auto_increment, deptno mediumint u ...

  10. 【mongo】用户添加、导入数据库、连接VUE

    添加用户 1.安装mongo时最好用apt-get install  因为这样可以省去很多麻烦,比如一些环境变量,还有一些文档路径等等的问题 2.确认一下自己的mongodb和mongodb-clie ...