forked from doocs/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.py
31 lines (30 loc) · 1016 Bytes
/
Solution.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:
def longestConsecutive(self, root: TreeNode) -> int:
def dfs(root):
if root is None:
return [0, 0]
nonlocal ans
incr = decr = 1
i1, d1 = dfs(root.left)
i2, d2 = dfs(root.right)
if root.left:
if root.left.val + 1 == root.val:
incr = i1 + 1
if root.left.val - 1 == root.val:
decr = d1 + 1
if root.right:
if root.right.val + 1 == root.val:
incr = max(incr, i2 + 1)
if root.right.val - 1 == root.val:
decr = max(decr, d2 + 1)
ans = max(ans, incr + decr - 1)
return [incr, decr]
ans = 0
dfs(root)
return ans