Binary Tree Longest Consecutive Sequence

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.

分析:

  可看作是二分树上的动态规划,自底向上和自顶向下DFS均可

代码:

int height(TreeNode *node, int &totalmax) {
if(!node)
return ;
//自底向上DFS
int leftlength = height(node->left, totalmax), rightlength = height(node->right, totalmax);
if(node->left && node->left->val - != node->val)
leftlength = ;
if(node->right && node->right->val - != node->val)
rightlength = ;
int nodemax = max(leftlength + , rightlength + );
totalmax = max(totalmax, nodemax);
return nodemax;
}
int path(TreeNode *root) {
int totalmax = INT_MIN;
height(root, totalmax);
return totalmax;
}

[Locked] 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. [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. [LeetCode] Binary Tree Longest Consecutive Sequence 二叉树最长连续序列

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

  9. Binary Tree Longest Consecutive Sequence

    Given a binary tree, find the length of the longest consecutive sequence path (连续的路径,不是从小到大). The pa ...

随机推荐

  1. 怎么捕获和记录SQL Server中发生的死锁

    我们知道,可以使用SQL Server自带的Profiler工具来跟踪死锁信息.但这种方式有一个很大的敝端,就是消耗很大.据国外某大神测试,profiler甚至可以占到服 务器总带宽的35%,所以,在 ...

  2. eclipse中更改默认编码格式

    更改过程如下: (1)window->preferences->general->content Types, 选中java class file修改default encoding ...

  3. SQL中使用的一些函数问题

    abs()取绝对值ceil()上取整floor()下取整initcap()使串中的所有单词的首字母变为大写substr()取子串 这些函数都是oracle的sql内置函数.

  4. 改进《完美让IE兼容input placeholder属性的jquery实现》的不完美

    <完美让IE兼容input placeholder属性的jquery实现>中的代码在IE9以下浏览器中会出错,原因是因为ie9以下的浏览器对input的标签的解释不同. 例如对以下文本框的 ...

  5. SGU 137.Funny String

    题目描述 一个序列S1 S2 S3... Sn 如果满足 新序列 S1-1 S2 S3 ...Sn+1能够通过旋转的操作(不是翻转)来得到旧的序列,那么这个序列就叫做Funny序列.例如 1 2 1 ...

  6. 动态树LCT小结

    最开始看动态树不知道找了多少资料,总感觉不能完全理解.但其实理解了就是那么一回事...动态树在某种意思上来说跟树链剖分很相似,都是为了解决序列问题,树链剖分由于树的形态是不变的,所以可以通过预处理节点 ...

  7. ARM平台的内核模块编写与安装

       Linux 系统一直在不断地发展,而相应地她的代码量也在不断的增大,直接导致的结果就是她的可执行镜像就变得越来越庞大.那么问题来了,如果将所有的镜像文件一次性地复制到内存中,那么所需的空间就非常 ...

  8. C#编程连接数据库,通过更改配置文件切换数据库功能。

           该实例主要应用情景:假如某公司用mysql当做数据库服务器,由于发现mysql数据库在运行中出现不稳定情况,针对这情况,厂家要求更换连接数据库方式,改用SQL server数据库,来满足 ...

  9. 一个基于nodejs,支持http/https的中间人(MITM)代理,便于渗透测试和开发调试。

    源码地址:https://github.com/wuchangming/node-mitmproxy node-mitmproxy node-mitmproxy是一个基于nodejs,支持http/h ...

  10. 生成订单唯一id

    $yCode = array('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J'); $orderSn = $yCode[intval(date('Y') ...