-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path501.py
More file actions
executable file
·31 lines (29 loc) · 972 Bytes
/
501.py
File metadata and controls
executable file
·31 lines (29 loc) · 972 Bytes
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 dfs(self, root, lastV):
leftNum = 0
rightNum = 0
if (root.left):
leftNum = self.dfs(root.left, root.val)
if (root.right):
rightNum = self.dfs(root.right, root.val)
if (1 + leftNum + rightNum) > self.count:
self.count = 1 + leftNum + rightNum
self.res = [root.val]
else:
if (1 + leftNum + rightNum) == self.count:
self.res.append(root.val)
if (root.val == lastV):
return (1 + leftNum + rightNum)
else:
return 0
def findMode(self, root: Optional[TreeNode]) -> List[int]:
self.res = []
self.count = 0
self.dfs(root, root.val)
return self.res