-
-
Notifications
You must be signed in to change notification settings - Fork 298
/
Copy path687.py
64 lines (61 loc) · 2.26 KB
/
687.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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
__________________________________________________________________________________________________
sample 336 ms submission
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class Solution:
def helper(self, node):
cur_path=0
if node.left:
n, val = self.helper(node.left)
if val==node.val:
cur_path = n+1
if self.longest_path<cur_path:
self.longest_path = cur_path
if node.right:
n, val = self.helper(node.right)
if val==node.val:
cur_path2 = n+1
if self.longest_path<(cur_path+cur_path2):
self.longest_path = (cur_path+cur_path2)
cur_path = max(cur_path, cur_path2)
return cur_path, node.val
def longestUnivaluePath(self, root: TreeNode) -> int:
self.longest_path = 0
if root:
self.helper(root)
return self.longest_path
__________________________________________________________________________________________________
sample 17088 kb submission
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class Solution:
def longestUnivaluePath(self, root: TreeNode) -> int:
longestPath = [float('-inf')]
def longestCommonPath(root, val):
if not root:
return 0
left = right = 0
found = False
if root.val == val:
found = True
left = longestCommonPath(root.left, val)
right = longestCommonPath(root.right, val)
else:
left = longestCommonPath(root.left, root.val)
right = longestCommonPath(root.right, root.val)
if left+right+1 > longestPath[0]:
longestPath[0] = left+right+1
return max(left+1, right+1) if found else 0
if not root:
return 0
longestCommonPath(root, root.val)
return longestPath[0]-1
__________________________________________________________________________________________________