[Locked] Binary Tree Longest Consecutive Sequence
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的更多相关文章
- LeetCode Binary Tree Longest Consecutive Sequence
原题链接在这里:https://leetcode.com/problems/binary-tree-longest-consecutive-sequence/ 题目: Given a binary t ...
- [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 ...
- LeetCode 549. Binary Tree Longest Consecutive Sequence II
原题链接在这里:https://leetcode.com/problems/binary-tree-longest-consecutive-sequence-ii/description/ 题目: G ...
- LeetCode 298. Binary Tree Longest Consecutive Sequence
原题链接在这里:https://leetcode.com/problems/binary-tree-longest-consecutive-sequence/ 题目: Given a binary t ...
- [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 ...
- [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 ...
- [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 ...
- [LeetCode] Binary Tree Longest Consecutive Sequence 二叉树最长连续序列
Given a binary tree, find the length of the longest consecutive sequence path. The path refers to an ...
- Binary Tree Longest Consecutive Sequence
Given a binary tree, find the length of the longest consecutive sequence path (连续的路径,不是从小到大). The pa ...
随机推荐
- DATEDIFF interval=ms的用法
datediff(ms,@CurrDateTime,@Date)>0 当上面的日期超过24天,用上面的sql会有问题 要修改成如下: (CONVERT(VARCHAR,@CurrDateTime ...
- [LeetCode OJ]-Climbing Stairs
You are climbing a stair case. It takes n steps to reach to the top. Each time you can either climb ...
- POJ 1236.Network of Schools (强连通)
首先要强连通缩点,统计新的图的各点的出度和入度. 第一问直接输出入度为0的点的个数 第二问是要是新的图变成一个强连通图,那么每一个点至少要有一条出边和一条入边,输出出度和入度为0的点数大的那一个 注意 ...
- DOM对象控制HTML无素——详解2
节点属性 在文档对象模型 (DOM) 中,每个节点都是一个对象.DOM 节点有三个重要的属性 : 1. nodeName : 节点的名称 2. nodeValue :节点的值 3. nodeType ...
- 入门3:PHP环境开发搭建(windows)
一.环境需要 硬件环境(最低配置): 双核CPU 8G内存 操作系统环境: Windows(64位)7+ Mac OS X 10.10+ Linux 64位(推荐Ubuntu 14 LTS) /**拓 ...
- Lua 5.1 for Delphi 2010
This is a Lua 5.1 Wrapper for Delphi 2009 and Delphi 2010 which automatically creates OOP callback f ...
- python类class基础
44.class类: 一.类定义的一般形式: 1.简单的形式:实例化对象没有自己独有 ...
- Rewrite的QSA是什么意思?
原版的英文: When the replacement URI contains a query string, the default behavior of RewriteRule is to d ...
- iOS开源项目
在结束了GitHub平台上“最受欢迎的Android开源项目”系列盘点之后,我们正式迎来了“GitHub上最受欢迎的iOS开源项目”系列盘点.今天,我们将介绍20个在GitHub上非常受开发者欢迎的i ...
- 转:2014 年 15 款新评定的最佳 PHP 框架
原文来自于:http://blog.jobbole.com/59999/ 原文出处: codegeekz 译文出处:oschina 欢迎分享原创到伯乐头条 通常,框架都会被认为是帮助开发者快速 ...