原题链接在这里: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的更多相关文章

  1. [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 ...

  2. [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 ...

  3. [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 ...

  4. LeetCode 298. Binary Tree Longest Consecutive Sequence

    原题链接在这里:https://leetcode.com/problems/binary-tree-longest-consecutive-sequence/ 题目: Given a binary t ...

  5. [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 ...

  6. [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 ...

  7. LeetCode Binary Tree Longest Consecutive Sequence

    原题链接在这里:https://leetcode.com/problems/binary-tree-longest-consecutive-sequence/ 题目: Given a binary t ...

  8. [Locked] Binary Tree Longest Consecutive Sequence

    Binary Tree Longest Consecutive Sequence Given a binary tree, find the length of the longest consecu ...

  9. [LeetCode] Binary Tree Longest Consecutive Sequence 二叉树最长连续序列

    Given a binary tree, find the length of the longest consecutive sequence path. The path refers to an ...

随机推荐

  1. Kafka消息文件存储

    在对消息进行存储和缓存时,Kafka依赖于文件系统.(Page Cache) 线性读取和写入是所有使用模式中最具可预计性的一种方式,因而操作系统采用预读(read-ahead)和后写(write-be ...

  2. fio测试freenas共享的iscsi磁盘性能

    4k随机读iops: fio -ioengine=libaio -bs=4k -direct=1 -thread -rw=randread -filename=/dev/sdb -iodepth=32 ...

  3. 1-22-shell脚本基本应用-实验手册

    脚本应用思路 1. 确定命令操作(设计并执行任务) 2. 编写Shell脚本(组织任务过程) 3. 设置计划任务(控制时间,调用任务脚本) ------------------------------ ...

  4. CodeForces 297A Parity Game (脑补题)

    题意 一个01串,可以有两种操作:①在末尾添加parity(a):②删除开头的一个字符.其中parity(a),当串中1的个数为奇数时为1,偶数时为0.问某个01串是否可以通过若干操作变成另一个01串 ...

  5. my.cnf 参数说明

    [mysql] prompt="\\u@\\h:\p  \\R:\\m:\\s [\\d]>"  The prompt command reconfigures the de ...

  6. 关于Mac上使用ideviceinstaller操作iPhoneXR等24位UDID设备报“ERROR: Invalid UDID specified”解决办法

    最近新申请了一台iPhone XR, 测试时发现使用ideviceinstaller命令老是报错: Jackeys-MacBook-Pro:~ jackey$ ideviceinstaller -u ...

  7. WTH统计

    SELECT t2.MasterName AS '类型',SUM(t1.DailyCount) AS '数量',(CASE T2.MasterName WHEN '电子阅读' THEN '篇' WHE ...

  8. Prism 4 文档 ---第4章 模块化应用程序开发

    模块化应用程序是指将一个应用程序拆分成一系列的可以组合的功能单元.一个客户端模块封装了应用程序的一部分,并且通常是一系列相关的关注点.它可以包含一个相关的组件的集合,就像用户界面,应用程序功能,和一些 ...

  9. fzu Problem 2275 Game(kmp)

    Problem 2275 Game Accept: 62    Submit: 165Time Limit: 1000 mSec    Memory Limit : 262144 KB  Proble ...

  10. ES查询index对应的mapping信息

    private void getMappingByIndex(String indices) throws IOException { GetMappingsRequest getMappingsRe ...