[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 ...
随机推荐
- 如何修改 VIM 制表符的空格数?
想修改一下编辑器vi里的制表符(Tab)的空格数.因为它默认的太长(默认是8个空格). 在网上搜到了这篇文章http://my.oschina.net/captaintheron/blog/515 ...
- sencha touch 模仿tabpanel导航栏TabBar(2013-11-7)
基于sencha touch 2.2所写 代码: /* *模仿tabpanel导航栏 */ Ext.define('ux.TabBar', { alternateClassName: 'tabBar' ...
- 部署OpenStack问题汇总(三)--Failed to add image
使用glance add 上传完img文件的时候出现了下面的错误 ------------------------------------------------------------------- ...
- centos6上使用xfs文件系统
ext4目前也还没有真的支持16TB以上的单分区空间,由于工具的限制,只能创建最大为16T的单分区决定直接用xfs 安装xfs [root@ ~]$ yum install kmod-xfs xfsp ...
- C# 二维码 ThoughtWorks.QRCode.dll
ThoughtWorks.QRCode.dll: 1.ThoughtWorks.QRCode.dll 2.通过 NuGet 添加 后台代码: using System; using System.Dr ...
- 23种设计模式之外观模式(Facade)
外观模式是对象的结构模式,要求外部与一个子系统的通信必须通过一个统一的外观对象进行,为子系统中的一组接口提供一个一致的界面,外观模式定义了一个高层接口,这个接口使得这一子系统更加容易使用. 优点: 1 ...
- Ubuntu 16.04系统下apt-get和dpkg区别
两者的区别是dpkg绕过apt包管理数据库对软件包进行操作,所以你用dpkg安装过的软件包用apt可以再安装一遍,系统不知道之前安装过了,将会覆盖之前dpkg的安装.1.dpkg是用来安装.deb文件 ...
- Mysql----索引原理与慢查询优化
一 介绍 为何要有索引? 一般的应用系统,读写比例在10:1左右,而且插入操作和一般的更新操作很少出现性能问题,在生产环境中,我们遇到最多的,也是最容易出问题的,还是一些复杂的查询操作,因此对查询语句 ...
- Mapreduce 原理及程序分析
1.MapReduce(Map+Reduce) 提出一个问题: 目标:你想数出一摞牌中有多少张黑桃. 直观方式:一张一张检查并且数出有多少张是黑桃数目 MapReduce方法则是: 给在座的所有玩家中 ...
- Django运算符表达式
在html页面中,加入运算符表达式,进行逻辑判断.可参考手册.我用的Django是2.1版本 view.py中的代码: from django.shortcuts import render from ...