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 ...
随机推荐
- js执行环境的周边概念
一.熟悉几个名词: 1.执行环境(execution context),也叫执行上下文,每个函数都会有自己的执行环境:当浏览器首次加载脚本时,他将默认进入全局执行环境:如果接下来要调用一个内部函数,则 ...
- 明确出需求 然后开会评审 要什么接口 接口参数、返回json内容、格式 协定好 在做
明确出需求 然后开会评审 要什么接口 接口参数.返回json内容.格式 协定好 在做
- tail命令 | head命令
tail -f -n 50 log.txt 循环读取文件log.txt的后50行 head -n 50 log.txt 显示文件的前n行
- ubuntu中python2与python3的默认启动切换
方法摘自SegmentFault: 方法一: echo alias python=python3 >> ~/.bashrc && source ~/.bashrc相当于先打 ...
- Comprehensive Python Cheatsheet
ToC = { '1. Collections': [List, Dict, Set, Range, Enumerate, Namedtuple, Iterator, Generator], '2. ...
- Post with HttpClient4
转载:http://www.cnblogs.com/luxiaoxun/p/6165237.html 作者:阿凡卢 出处:http://www.cnblogs.com/luxiaoxun/ HttpC ...
- fzu Problem 2275 Game(kmp)
Problem 2275 Game Accept: 62 Submit: 165Time Limit: 1000 mSec Memory Limit : 262144 KB Proble ...
- Sumlime text3 安装包、汉化包、注册码
Sumlime text3 安装包.汉化包.注册码 http://files.cnblogs.com/files/panmy/%E5%9C%86%E8%A7%92.rar
- LeetCode OJ:Restore IP Addresses(存储IP地址)
Given a string containing only digits, restore it by returning all possible valid IP address combina ...
- VirtualBox安装Redhat9.0
1.准备软件 虚拟机:VirtualBox-4.3.8-92456-Win.exe RedHat:shrike-i386-disc1.iso shrike-i386-disc2.iso ...