Skip to content

Commit 5089cb0

Browse files
authored
Create binary_tree_longest_consecutive_sequence.py
1 parent e2a6b58 commit 5089cb0

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
class Solution:
2+
"""
3+
@param root: the root of binary tree
4+
@return: the length of the longest consecutive sequence path
5+
"""
6+
def longestConsecutive(self, root):
7+
# write your code here
8+
return Solution.longest(root, None, 0)
9+
10+
@staticmethod
11+
def longest(node, parent, curr_len):
12+
if not node:
13+
return 0
14+
if parent and ((parent.val + 1) == node.val):
15+
curr_len = curr_len + 1
16+
else:
17+
curr_len = 1
18+
left_len = Solution.longest(node.left, node, curr_len)
19+
right_len = Solution.longest(node.right, node, curr_len)
20+
return max(curr_len, max(left_len, right_len))
21+
22+
# easy: https://www.lintcode.com/problem/binary-tree-longest-consecutive-sequence/

0 commit comments

Comments
 (0)