[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.
Especially, this path can be either increasing or decreasing. For example, [1,2,3,4] and [4,3,2,1] are both considered valid, but the path [1,2,4,3] is not valid. On the other hand, the path can be in the child-Parent-child order, where not necessarily be parent-child order.
Example 1:
- Input:
- 1
- / \
- 2 3
- Output: 2
- Explanation: The longest consecutive path is [1, 2] or [2, 1].
Example 2:
- Input:
- 2
- / \
- 1 3
- Output: 3
- Explanation: The longest consecutive path is [1, 2, 3] or [3, 2, 1].
Note: All the values of tree nodes are in the range of [-1e7, 1e7].
这个题目思路实际上跟 [LeetCode] 124. Binary Tree Maximum Path Sum_ Hard tag: DFS recursive很像, 只是要注意的是当可以两者结合起来的时候, 方向要一致.
1. Constraints
1) empty => 0
2. Ideas
DFS T: O(n) S: O(n)
1) edge case
2) helper function, get number of nodes of longest increasing path and longest decreasing path, each time compare self.ans with 1 + li + rd, 1+ ri + ld
3) return self.ans
3. Code
- class Solution:
- def longestConsecutive2(self, root):
- self.ans = 0
- def helper(root):
- if not root: return 0, 0
- li, ld = helper(root.left)
- ri, rd = helper(root.right)
- li = li if li and root.left.val + 1 == root.val else 0
- ld = ld if ld and root.left.val - 1 == root.val else 0
- ri = ri if ri and root.right.val + 1 == root.val else 0
- rd = rd if rd and root.right.val -1 == root.val else 0
- self.ans = max(self.ans, 1+ li + rd, 1 + ri + ld)
- return 1+ max(li, ri), 1+ max(ld, rd)
- helper(root)
- return self.ans
4. Test cases
1)empty
2) 1
3)
- 2
- / \
- 1 3
[LeetCode] 549. Binary Tree Longest Consecutive Sequence II_ Medium tag: DFS recursive的更多相关文章
- [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] 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 ...
- [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
原题链接在这里: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 ...
- [Locked] Binary Tree Longest Consecutive Sequence
Binary Tree Longest Consecutive Sequence Given a binary tree, find the length of the longest consecu ...
- [LeetCode] Binary Tree Longest Consecutive Sequence 二叉树最长连续序列
Given a binary tree, find the length of the longest consecutive sequence path. The path refers to an ...
随机推荐
- 【推荐系统论文笔记】Introduction To Recommender Systems: Algorithms and Evaluation
这篇论文比较短,正如题目所说,主要还是简单地介绍了一下推荐系统的一些算法以及评估的方法. 推荐系统之前是基于关键字信息的过滤系统,后来发展成为协同过滤系统,解决了两个问题:1.通过人工审核去评价那些具 ...
- 告知你不为人知的UDP-连接性和负载均衡
版权声明:本文由黄日成原创文章,转载请注明出处: 文章原文链接:https://www.qcloud.com/community/article/812444001486438028 来源:腾云阁 h ...
- sencha touch carousel 扩展 CardList 可绑定data/store
扩展代码: /* *扩展carousel *通过data,tpl,store配置数据 */ Ext.define('ux.CardList', { extend: 'Ext.carousel.Caro ...
- 第四步 使用 adt-eclipse 打包 Cordova (3.0及其以上版本) + sencha touch 项目
cordova最新中文api http://cordova.apache.org/docs/zh/3.1.0/ 1.将Cordova 生成的项目导入到adt-eclipse中,如下: 项目结构如下: ...
- FileInputStream 和 FileOutputStream
简介 FileInputStream和FileOutputStream都是用来处理二进制数据源磁盘文件的流的. 他们分别派生自顶层抽象类InputStream和OutputStream FileInp ...
- [心跳] 使用心跳机制实现CS架构下多客户端的在线状态实时更新以及掉线自动重连
此文讲述的内容是一个实际项目开发中的一部分内容,笔者将亲身经历写成文章. [背景] 现 需要实现这样的功能:有多个客户端连着同一个服务器.服务器和客户端之间需要“互相”知道彼此的连接状态.比如在某一时 ...
- &与&&, |与||区别
&和|称为短逻辑符,&&及||称为长逻辑符.长逻辑符只比较左边和右边的第一个元素,而短逻辑符会比较所有的 > a<-c(TRUE, FALSE, TRUE, FAL ...
- 使用find命令按条件查找多个文件并且拷贝至指定目录
命令格式如下 find / \( -name "*.war" -o -name "*.jar" \) | xargs -i cp {} ${wardir} 当需 ...
- opencv3在CMakeLists.txt中的调用问题
在cmake工程中使用opencv需要在CMakeLists.txt文件中加以调用,在opencv2.xx版本,可以用以下语句 # 寻找OpenCV库 find_package( OpenCV REQ ...
- ZOJ 4029 - Now Loading!!! - [前缀和+二分]
题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=4029 Time Limit: 1 Second Memory L ...