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:

  1. Input:
  2. 1
  3. / \
  4. 2 3
  5. Output: 2
  6. Explanation: The longest consecutive path is [1, 2] or [2, 1].

Example 2:

  1. Input:
  2. 2
  3. / \
  4. 1 3
  5. Output: 3
  6. 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

  1. class Solution:
  2. def longestConsecutive2(self, root):
  3. self.ans = 0
  4. def helper(root):
  5. if not root: return 0, 0
  6. li, ld = helper(root.left)
  7. ri, rd = helper(root.right)
  8. li = li if li and root.left.val + 1 == root.val else 0
  9. ld = ld if ld and root.left.val - 1 == root.val else 0
  10. ri = ri if ri and root.right.val + 1 == root.val else 0
  11. rd = rd if rd and root.right.val -1 == root.val else 0
  12. self.ans = max(self.ans, 1+ li + rd, 1 + ri + ld)
  13. return 1+ max(li, ri), 1+ max(ld, rd)
  14. helper(root)
  15. return self.ans

4. Test cases

1)empty

2) 1

3)

  1. 2
  2. / \
  3. 1 3

[LeetCode] 549. Binary Tree Longest Consecutive Sequence II_ Medium tag: DFS recursive的更多相关文章

  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

    原题链接在这里:https://leetcode.com/problems/binary-tree-longest-consecutive-sequence-ii/description/ 题目: G ...

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

  6. LeetCode Binary Tree Longest Consecutive Sequence

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

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

  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. 【推荐系统论文笔记】Introduction To Recommender Systems: Algorithms and Evaluation

    这篇论文比较短,正如题目所说,主要还是简单地介绍了一下推荐系统的一些算法以及评估的方法. 推荐系统之前是基于关键字信息的过滤系统,后来发展成为协同过滤系统,解决了两个问题:1.通过人工审核去评价那些具 ...

  2. 告知你不为人知的UDP-连接性和负载均衡

    版权声明:本文由黄日成原创文章,转载请注明出处: 文章原文链接:https://www.qcloud.com/community/article/812444001486438028 来源:腾云阁 h ...

  3. sencha touch carousel 扩展 CardList 可绑定data/store

    扩展代码: /* *扩展carousel *通过data,tpl,store配置数据 */ Ext.define('ux.CardList', { extend: 'Ext.carousel.Caro ...

  4. 第四步 使用 adt-eclipse 打包 Cordova (3.0及其以上版本) + sencha touch 项目

    cordova最新中文api http://cordova.apache.org/docs/zh/3.1.0/ 1.将Cordova 生成的项目导入到adt-eclipse中,如下: 项目结构如下: ...

  5. FileInputStream 和 FileOutputStream

    简介 FileInputStream和FileOutputStream都是用来处理二进制数据源磁盘文件的流的. 他们分别派生自顶层抽象类InputStream和OutputStream FileInp ...

  6. [心跳] 使用心跳机制实现CS架构下多客户端的在线状态实时更新以及掉线自动重连

    此文讲述的内容是一个实际项目开发中的一部分内容,笔者将亲身经历写成文章. [背景] 现 需要实现这样的功能:有多个客户端连着同一个服务器.服务器和客户端之间需要“互相”知道彼此的连接状态.比如在某一时 ...

  7. &与&&, |与||区别

    &和|称为短逻辑符,&&及||称为长逻辑符.长逻辑符只比较左边和右边的第一个元素,而短逻辑符会比较所有的 > a<-c(TRUE, FALSE, TRUE, FAL ...

  8. 使用find命令按条件查找多个文件并且拷贝至指定目录

    命令格式如下 find / \( -name "*.war" -o -name "*.jar" \) | xargs -i cp {} ${wardir} 当需 ...

  9. opencv3在CMakeLists.txt中的调用问题

    在cmake工程中使用opencv需要在CMakeLists.txt文件中加以调用,在opencv2.xx版本,可以用以下语句 # 寻找OpenCV库 find_package( OpenCV REQ ...

  10. ZOJ 4029 - Now Loading!!! - [前缀和+二分]

    题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=4029 Time Limit: 1 Second Memory L ...