LeetCode 549. Binary Tree Longest Consecutive Sequence II
原题链接在这里:https://leetcode.com/problems/binary-tree-longest-consecutive-sequence-ii/description/
题目:
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].
题解:
与Binary Tree Longest Consecutive Sequence类似.
采用bottom-up的方法dfs. 每个点同时维护能向下延展的最大increasing 和 decreasing长度.
Time Complexity: O(n). Space: O(logn).
AC Java:
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
int max = 0;
public int longestConsecutive(TreeNode root) {
dfs(root);
return max;
} private int[] dfs(TreeNode root){
if(root == null){
return new int[2];
} int inc = 1;
int dec = 1;
int [] l = dfs(root.left);
int [] r = dfs(root.right);
if(root.left != null){
if(root.left.val - 1 == root.val){
inc = Math.max(inc, l[0]+1);
}
if(root.left.val + 1 == root.val){
dec = Math.max(dec, l[1]+1);
}
} if(root.right != null){
if(root.right.val - 1 == root.val){
inc = Math.max(inc, r[0]+1);
}
if(root.right.val + 1 == root.val){
dec = Math.max(dec, r[1]+1);
}
} max = Math.max(max, dec+inc-1);
return new int[]{inc, dec};
}
}
LeetCode 549. Binary Tree Longest Consecutive Sequence II的更多相关文章
- [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_ Medium tag: DFS recursive
Given a binary tree, you need to find the length of Longest Consecutive Path in Binary Tree. Especia ...
- [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] Binary Tree Longest Consecutive Sequence 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
原题链接在这里:https://leetcode.com/problems/binary-tree-longest-consecutive-sequence/ 题目: Given a binary t ...
- [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 ...
随机推荐
- Oracle数据库报错: ORA-29275:部分多字节字符
Oracle数据库报错: ORA-29275:部分多字节字符 就是你查出来的字符串(有汉字的)有可能会出问题, 在认为有问题的字段 用 to_nchar("字段")包起来 比如 你 ...
- jquery阻止冒泡和阻止默认事件
event.stopPropagation(); event.preventDefault(); http://www.cnblogs.com/qixuejia/archive/2013/10/10/ ...
- C++(二十五) — 类的封装、实现
1.类的封装.实现.对象的定义及使用 (1)类是一组对象的抽象化模型.类对象将数据及函数操作集合在一个实体中,只需要接口,而不需要知道具体的操作. 隐藏细节,模型化: 类内自由修改: 减少耦合,相当于 ...
- 进程上下文频繁切换导致load average过高
一.问题现象 现网有两台虚拟机主机95%的cpu处于idle状态,内存使用率也不是特别高,而主机的load average达到了40多. 二.问题分析 先在主机上通过top.free.ps.iosta ...
- ASP.NET网站使用Kindeditor富文本编辑器配置步骤
1. 下载编辑器 下载 KindEditor 最新版本,下载页面: http://www.kindsoft.net/down.php 2. 部署编辑器 解压 kindeditor-x.x.x.zip ...
- JavaScript中伪协议
javascript:这个特殊的协议类型声明了URL的主体是任意的javascript代码,它由javascript的解释器运行 将javascript代码添加到客户端的方法是把它放置在伪协议说明符j ...
- PostgreSQL基于时间点故障恢复PITR( point-in-time recovery )
PostgreSQL在使用过程中经常会发生一些失误的操作,但往往是可以弥补的.但是如果真遇到了无法挽回的误操作,只能寄希望于有备份了. 接下来的故障恢复也是基于有备份的情况,没有备份的情况,目前还没有 ...
- zookeeper数据一致性与paxos算法
数据一致性与paxos算法 据说Paxos算法的难理解与算法的知名度一样令人敬仰,所以我们先看如何保持数据的一致性,这里有个原则就是: 在一个分布式数据库系统中,如果各节点的初始状态一致,每个节点都执 ...
- java事务(二)
本地事务 事务类型 事务可以分为本地事务和分布式事务两种类型.这两种事务类型是根据访问并更新的数据资源的多少来进行区分的.本地事务是在单个数据源上进行数据的访问和更新,而分布式事务是跨越多个数据源来进 ...
- HM编码器代码阅读(1)——介绍以及相关知识
HM是HEVC(H.265)的开源实现,可以从网上直接下载.HEVC(H.265)是新一代的视频编解码标准.本人目前研究的只是编码器部分,而且还是入门阶段!为了提高自己,边学边记,由于理解不够深入,难 ...