Given a binary tree, find the length of the longest consecutive sequence path.

The path refers to any sequence of nodes from some starting node to any node in the tree along the parent-child connections. The longest consecutive path need to be from parent to child (cannot be the reverse).

Example 1:

Input:

   1
\
3
/ \
2 4
\
5 Output: 3 Explanation: Longest consecutive sequence path is 3-4-5, so return 3.

Example 2:

Input:

   2
\
3
/
2
/
1 Output: 2 Explanation: Longest consecutive sequence path is 2-3, not 3-2-1, so return 2.

这个题目思路跟[LeetCode] 687. Longest Univalue Path_Easy tag: DFS recursive, 但实际上比它还简单, 因为不需要验证l + root + r, 但是也只是self.ans 里面少加一个判断而已, 本质一样, 用helper function, 得到包括root 的最大的increasing consecutive path 的 number of nodes, 并且每次跟self.ans 比较, 最后return self.ans.

1. Constraints

1) empty => 0

2) element will be intergeres

2) Ideas

DFS    T; O(n)    S: O(n)

3) code

class Solution:
def longestConsecutive(self, root):
ans = [0]
def longestConsecutiveWithRoot(root):
if not root: return 0
l, r = longestConsecutiveWithRoot(root.left), longestConsecutiveWithRoot(root.right)
l = l if root.left and root.left.val - 1 == root.val else 0
r = r if root.right and root.right.val - 1 == root.val else 0
local = 1 + max(l, r)
ans[0] = max(ans[0], local)
longestConsecutiveWithRoot(root)
return ans[0]

4. Test cases

   1
\
3
/ \
2 4
\
5

[LeetCode] 298. Binary Tree Longest Consecutive Sequence_Medium tag: DFS recursive的更多相关文章

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

  2. LeetCode 298. Binary Tree Longest Consecutive Sequence

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

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

  4. LeetCode 549. Binary Tree Longest Consecutive Sequence II

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

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

  6. 298. Binary Tree Longest Consecutive Sequence

    题目: Given a binary tree, find the length of the longest consecutive sequence path. The path refers t ...

  7. 298. Binary Tree Longest Consecutive Sequence最长连续序列

    [抄题]: Given a binary tree, find the length of the longest consecutive sequence path. The path refers ...

  8. [LC] 298. Binary Tree Longest Consecutive Sequence

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

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

随机推荐

  1. 【Shell脚本编程系列】Shell脚本开发的习惯和规范

    1.开头指定脚本解释器 #!/bin/sh或#!/bin/bash 2.开头加版本版权信息 #Date #Author #Mail #Function #Version 提示:可配置vim编辑文件时自 ...

  2. Android 编译时:m、mm、mmm、mma、mmma的区别

    m:编译整个安卓系统 makes from the top of the tree mm:编译当前目录下的模块,当前目录下需要有Android.mk这个makefile文件,否则就往上找最近的Andr ...

  3. 使用maven项目 转XSD、 WSDL 成Java类

    因为项目需要,在网上查找了很多WSDL的使用方式,但是感觉不是特别顺利.现在是利用Maven 项目快速转化的方法. 首先建立一个maven 项目 ,并将下面的pom文件复制进去. <projec ...

  4. CSS+transform画动态表情

    先给大家看下画完后是什么样子: 代码看这里: html代码: <body> <div class="emoji emoji_like"> <div c ...

  5. isolinux.cfg 文件是干什么的

    1.   首先光盘镜像也就是iso文件采用的是“ISO 9660 ”文件系统 . cd上的文件都存在这个简单的iso文件系统里,linux可以用mount  -o loop 直接把*.iso文件mou ...

  6. 23种设计模式之建造者模式(Builder)

    建造者模式强调将一个复杂对象的创建与它的表示分离,使得同样的构建过程可以创建不同的表示.建造者模式是一步一步地创建一个复杂的对象,它允许用户只通过制定复杂对象的类型和内容就可以构建它们,用户不需要知道 ...

  7. [HTML5]移动平台的HTML5开发框架

    jQuery Mobile http://jquerymobile.com/ jQTouch http://jqtouch.com/ DHTMLX Touch http://dhtmlx.com/to ...

  8. iOS - 使用苹果自带的UIVideoEditController进行视频编辑

    UIVideoEditorController类包含了由系统提供的界面,使用户可以交互式的剪切视频.UIVideoEditorController对象处理用户的交互并且提供把编辑后的视频的文件系统路径 ...

  9. 思科SVI接口和路由接口区别

    Cisco多层交换中提到了一个SVI接口,路由接口.在多层交换机上可以将端口配置成不同类型的接口. 其中SVI接口 类似于  interface Vlan10ip address 192.168.20 ...

  10. Python2.7设置在shell脚本中自动补全功能的方法

    1.新建tab.py文件 #!/usr/bin/env python # python startup file import sys import readline import rlcomplet ...