[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 ...
随机推荐
- Delphi XE开发 Android 开机自动启动
https://blog.csdn.net/tanqth/article/details/74357209 Android 下的广播 在Android下,要让我们开发的APP能在开机时自动启动,必须使 ...
- SQL Server Profiler工具【转】
一.SQL Profiler工具简介 转自:http://www.cnblogs.com/kissdodog/p/3398523.html SQL Profiler是一个图形界面和一组系统存储过程,其 ...
- 【大数据系列】hive安装及启动
一.安装好jdk和hadoop 二.下载apache-hive https://mirrors.tuna.tsinghua.edu.cn/apache/hive/hive-2.3.0/ 三.解压到安装 ...
- vue - 父组件数据变化控制子组件类名切换
先说当时的思路和实现核心是父子组件传值和v-bind指令动态绑定class实现 1. 父组件引用.注册.调用子组件script中引用 import child from '../components/ ...
- VS自动添加头部注释
让VS自动生成类的头部注释,只需修改两个文集即可,一下两个路径下个有一个 Class.cs文件 D:\Program Files (x86)\Microsoft Visual Studio 14.0\ ...
- Python 安装出错:Setup script exited with error: command 'gcc' failed with exit status 1
退出当前环境: logout (再重新登录进去) yum install python-devel -yyum install libevent-devel -y 把环境更新下yum instal ...
- Node学习HTTP模块(HTTP 服务器与客户端)
Node学习HTTP模块(HTTP 服务器与客户端) Node.js 标准库提供了 http 模块,其中封装了一个高效的 HTTP 服务器和一个简易的HTTP 客户端.http.Server 是一个基 ...
- Xcode - xcode-select: error: tool 'xcodebuild' requires Xcode报错解决方案
用mac 自带的终端执行的命令,安装安装Vapor和toolbox 安装指令: macdeMacBook-Pro:~ mac$ curl -sL check.vapor.sh| bash 结果报这个错 ...
- windows下java开发资料汇总
开发环境搭建: (1) java开发环境配置 (2) maven环境快速搭建 项目部署: (1) Eclipse中项目部署方法 (2) 使用Eclipse构建Maven ...
- HDU 1243 反恐训练营(最长公共序列)
反恐训练营 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submiss ...