[LeetCode] 298. Binary Tree Longest Consecutive Sequence_Medium tag: DFS recursive
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).
Example 1:
Input: 1
\
3
/ \
2 4
\
5 Output:3
Explanation: Longest consecutive sequence path is3-4-5
, so return3
.
Example 2:
Input: 2
\
3
/
2
/
1 Output: 2 Explanation: Longest consecutive sequence path is2-3
, not3-2-1
, so return2
.
这个题目思路跟[LeetCode] 687. Longest Univalue Path_Easy tag: DFS recursive, 但实际上比它还简单, 因为不需要验证l + root + r, 但是也只是self.ans 里面少加一个判断而已, 本质一样, 用helper function, 得到包括root 的最大的increasing consecutive path 的 number of nodes, 并且每次跟self.ans 比较, 最后return self.ans.
1. Constraints
1) empty => 0
2) element will be intergeres
2) Ideas
DFS T; O(n) S: O(n)
3) code
class Solution:
def longestConsecutive(self, root):
ans = [0]
def longestConsecutiveWithRoot(root):
if not root: return 0
l, r = longestConsecutiveWithRoot(root.left), longestConsecutiveWithRoot(root.right)
l = l if root.left and root.left.val - 1 == root.val else 0
r = r if root.right and root.right.val - 1 == root.val else 0
local = 1 + max(l, r)
ans[0] = max(ans[0], local)
longestConsecutiveWithRoot(root)
return ans[0]
4. Test cases
1
\
3
/ \
2 4
\
5
[LeetCode] 298. Binary Tree Longest Consecutive Sequence_Medium tag: DFS recursive的更多相关文章
- [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 298. Binary Tree Longest Consecutive Sequence
原题链接在这里:https://leetcode.com/problems/binary-tree-longest-consecutive-sequence/ 题目: Given a binary t ...
- [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 ...
- LeetCode 549. Binary Tree Longest Consecutive Sequence II
原题链接在这里:https://leetcode.com/problems/binary-tree-longest-consecutive-sequence-ii/description/ 题目: G ...
- [LeetCode] 549. Binary Tree Longest Consecutive Sequence II_ Medium tag: DFS recursive
Given a binary tree, you need to find the length of Longest Consecutive Path in Binary Tree. Especia ...
- 298. Binary Tree Longest Consecutive Sequence
题目: Given a binary tree, find the length of the longest consecutive sequence path. The path refers t ...
- 298. Binary Tree Longest Consecutive Sequence最长连续序列
[抄题]: Given a binary tree, find the length of the longest consecutive sequence path. The path refers ...
- [LC] 298. Binary Tree Longest Consecutive Sequence
Given a binary tree, find the length of the longest consecutive sequence path. The path refers to an ...
- [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 ...
随机推荐
- jQuery事件处理(七)
1.自定义事件(用户手动trigger的一般都是自定义事件) trigger: function( event, data, elem, onlyHandlers ) { var i, cur, tm ...
- 题目1440:Goldbach's Conjecture(哥达巴赫猜想)
题目链接:http://ac.jobdu.com/problem.php?pid=1440 详解链接:https://github.com/zpfbuaa/JobduInCPlusPlus 参考代码: ...
- 路由器子网掩码设置不正确导致github无法访问
奇怪的现象,路由器子网掩码设置成255.0.0.0会导致电脑访问不到https://github.com/ 改成默认的255.255.255.0就正常了.
- Struts2之web元素访问与模板包含与默认Action使用
上一篇为大家介绍了如何使用Action进行参数接收,以及简单的表单验证,本篇为大家介绍一下关于Action访问web元素的三种方式(request.session.application):模板包含( ...
- Visual Studio 2013安装Update 3启动crash的解决方法
Visual Studio 2013安装完Update 3后启动立刻crash,异常信息为: System.InvalidOperationException was unhandled Messag ...
- The request associated with the AsyncContext has already completed processing
Some time ago there was a problem with the servlet3.0, is in servlet in asynchronous processing data ...
- C语言qsort用法
一.对int类型数组排序 int num[100]; Sample: int cmp ( const void *a , const void *b ) { return *(int *)a - *( ...
- 使用COSBench工具对ceph s3接口进行压力测试
一.COSBench安装 COSBench是Intel团队基于java开发,对云存储的测试工具,全称是Cloud object Storage Bench 吐槽下,貌似这套工具是intel上海团队开发 ...
- npm使用报错解决办法
在使用脚手架工具进行项目搭建的时候,很多时候会用到npm ,最近用npm的时候遇到一个错误: 'CALL "I:\Program Files\nodejs\\node.exe" & ...
- Java 中运用DOS运行class(字节码)
附属: -dir:例举该目录的所有文件名称 有<dir>是文件夹,没有<dir>是文件-cd: 改变目录 进入其他目录 change direction-cd\:一次性回到根目 ...