Skip to content

Commit ce19b3c

Browse files
author
Partho Biswas
committed
298. Binary Tree Longest Consecutive Sequence
1 parent aafae21 commit ce19b3c

File tree

2 files changed

+64
-0
lines changed

2 files changed

+64
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -338,6 +338,7 @@ Check this [golden](https://tinyurl.com/ujopecz) post.
338338
|24| **[1110. Delete Nodes And Return Forest](https://tinyurl.com/rg8qomj)** | [Python](https://tinyurl.com/wu6rdaw/1110_Delete_Nodes_And_Return_Forest.py)| [Vid 1](https://tinyurl.com/t9rax3x), [Vid 2](https://tinyurl.com/uw6y3zk), [Art 1](https://tinyurl.com/ujopecz), [Art 2](https://tinyurl.com/tppglsu) | Medium | 📌 **TODO: Check again. Important |
339339
|25| [257. Binary Tree Paths](https://leetcode.com/problems/binary-tree-paths/)| [Python](https://tinyurl.com/wu6rdaw/257_Binary_Tree_Paths.py)| --- | Easy | DFS |
340340
|26| **[1145. Binary Tree Coloring Game](https://tinyurl.com/uukf2nm)**| [Python](https://tinyurl.com/wu6rdaw/1145_Binary_Tree_Coloring_Game.py)| [Art 1](https://tinyurl.com/v4k7jaj), [Art 2](https://tinyurl.com/rvwsjar) | Medium | DFS |
341+
|27| **[298. Binary Tree Longest Consecutive Sequence](https://tinyurl.com/rw2sqxr)**| [Python](https://tinyurl.com/wu6rdaw/298_Binary_Tree_Longest_Consecutive_Sequence.py)| **[Art 1](https://tinyurl.com/wmlq6md)** | Medium | DFS |
341342

342343
</p>
343344
</details>
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
# Definition for a binary tree node.
2+
# class TreeNode(object):
3+
# def __init__(self, x):
4+
# self.val = x
5+
# self.left = None
6+
# self.right = None
7+
8+
# Top-Down approach - Accepted
9+
class Solution(object):
10+
def longestConsecutive(self, root):
11+
"""
12+
:type root: TreeNode
13+
:rtype: int
14+
"""
15+
# if not root:
16+
# return 0
17+
longestSeque = 0
18+
longestSeque = self.longestConsecutiveHelper(root, None, 0, longestSeque)
19+
return longestSeque
20+
21+
def longestConsecutiveHelper(self, root, parent, currentLongestSeque, longestSeque):
22+
if not root:
23+
return max(currentLongestSeque, longestSeque)
24+
if parent and root.val == parent.val + 1:
25+
currentLongestSeque += 1
26+
else:
27+
currentLongestSeque = 1
28+
longestSeque = max(currentLongestSeque, longestSeque)
29+
leftLength = self.longestConsecutiveHelper(root.left, root, currentLongestSeque, longestSeque)
30+
rightLength = self.longestConsecutiveHelper(root.right, root, currentLongestSeque, longestSeque)
31+
maxLength = max(leftLength, rightLength, longestSeque)
32+
return maxLength
33+
34+
35+
36+
37+
# Bottom-Up approach - Accepted
38+
class Solution(object):
39+
def longestConsecutive(self, root):
40+
"""
41+
:type root: TreeNode
42+
:rtype: int
43+
"""
44+
if not root:
45+
return 0
46+
dummyNode = TreeNode(float("inf"))
47+
dummyNode.left = root
48+
longestSeque, val = self.longestConsecutiveHelper(dummyNode)
49+
return longestSeque
50+
51+
def longestConsecutiveHelper(self, root):
52+
if not root:
53+
return (0, float("-inf"))
54+
leftLength, leftVal = self.longestConsecutiveHelper(root.left)
55+
rightLength, rightVal = self.longestConsecutiveHelper(root.right)
56+
57+
if leftVal != float("-inf") and root.val == leftVal - 1:
58+
leftLength += 1
59+
elif rightVal != float("-inf") and root.val == rightVal - 1:
60+
rightLength += 1
61+
longestSeque = max(leftLength, rightLength, 1)
62+
63+
return (longestSeque, root.val)

0 commit comments

Comments
 (0)