Leetcode: Binary Tree Longest Consecutive Sequence

原创 2015年11月20日 18:21:40

Question

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

For example,
1
\
3
/ \
2 4
\
5
Longest consecutive sequence path is 3-4-5, so return 3.
2
\
3
/
2
/
1
Longest consecutive sequence path is 2-3,not3-2-1, so return 2.
Show Company Tags
Hide Tags Tree
Hide Similar Problems (H) Longest Consecutive Sequence


My solution

# Definition for a binary tree node.
# class TreeNode(object):
#     def __init__(self, x):
#         self.val = x
#         self.left = None
#         self.right = None

class Solution(object):
    def longestConsecutive(self, root):
        """
        :type root: TreeNode
        :rtype: int
        """

        self.length = 0
        self.helper(root)
        return self.length

    def helper(self, root):
        if root==None:
            return -1

        l = self.helper(root.left)
        r = self.helper(root.right)

        left  = True if root.left!=None and root.val == root.left.val -1 else False
        right = True if root.right!=None and root.val == root.right.val -1 else False

        if left and right:
            self.length = max( [self.length, 1+l, 1+r] )
            return max([1+l, 1+r])
        if left and not right:
            self.length = max( [self.length, 1+l] )
            return 1+l
        if not left and right:
            self.length = max( [self.length, 1+r] )
            return 1+r
        else:
            self.length = max( [self.length, 1] )
            return 1


版权声明:本文为博主原创文章,未经博主允许不得转载。

Leetcode-549. Binary Tree Longest Consecutive Sequence II

前言:为了后续的实习面试,开始疯狂刷题,非常欢迎志同道合的朋友一起交流。因为时间比较紧张,目前的规划是先过一遍,写出能想到的最优算法,第二遍再考虑最优或者较优的方法。如有错误欢迎指正。博主首发CSDN...
  • mcf171
  • mcf171
  • 2017年04月09日 11:23
  • 777

leetcode 298: Binary Tree Longest Consecutive Sequence

Binary Tree Longest Consecutive Sequence Total Accepted: 437 Total Submissions: 1351 Di...
  • xudli
  • xudli
  • 2015年10月29日 16:35
  • 4786

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

原文链接: http://www.cnblogs.com/grandyang/p/6864398.html class Solution { public: int longestCons...
  • github_38818603
  • github_38818603
  • 2017年10月09日 11:52
  • 96

[Leetcode]Binary Tree Longest Consecutive Sequence

Binary Tree Longest Consecutive Sequence Total Accepted: 437 Total Submissions: 1351 ...
  • zqh_1991
  • zqh_1991
  • 2016年03月22日 16:04
  • 452

[leetcode] 128. Longest Consecutive Sequence 解题报告

题目链接:https://leetcode.com/problems/longest-consecutive-sequence/ Given an unsorted array of intege...
  • qq508618087
  • qq508618087
  • 2016年02月25日 02:13
  • 1568

[LeetCode]128.Longest Consecutive Sequence

Longest Consecutive Sequence Given an unsorted array of integers, find the length of the longes...
  • SunnyYoona
  • SunnyYoona
  • 2014年01月17日 22:16
  • 1836

LeetCode 298. Binary Tree Longest Consecutive Sequence(二叉树最长连续序列)

原题网址:https://leetcode.com/problems/binary-tree-longest-consecutive-sequence/ Given a binary tree, ...
  • jmspan
  • jmspan
  • 2016年04月17日 00:08
  • 686

[python]leetcode(128). Longest Consecutive Sequence

problem Given an unsorted array of integers, find the length of the longest consecutive element...
  • PKU_Jade
  • PKU_Jade
  • 2017年09月10日 21:53
  • 87

Longest Consecutive Sequence Leetcode Python

Given an unsorted array of integers, find the length of the longest consecutive elements sequence. ...
  • hyperbolechi
  • hyperbolechi
  • 2015年01月19日 04:59
  • 299

LeetCode – Longest Consecutive Sequence Java

Given an unsorted array of integers, find the length of the longest consecutive elements sequence. ...
  • whywhom
  • whywhom
  • 2014年04月28日 08:29
  • 423
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:Leetcode: Binary Tree Longest Consecutive Sequence
举报原因:
原因补充:

(最多只允许输入30个字)