-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path0501_Find_Mode_in_Binary_Search_Tree.py
48 lines (41 loc) · 1.23 KB
/
0501_Find_Mode_in_Binary_Search_Tree.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
# 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 findMode(self, root: Optional[TreeNode]) -> List[int]:
num_dict = {}
# BSF Searching
def helper(_root:Optional[TreeNode]) -> None:
# Update dictionary
nonlocal num_dict
val = _root.val
if val in num_dict:
num_dict[val] += 1
else:
num_dict[val] = 1
# Leaf
if not (_root.left or _root.right):
return
# Left
if _root.left:
helper(_root.left)
# Right
if _root.right:
helper(_root.right)
return
# Run helper(BSF)
helper(root)
# Find max freq and keys
sol = float('-inf')
max_freq = 0
for key, freq in num_dict.items():
if freq > max_freq:
sol = [key]
max_freq = freq
elif freq == max_freq:
sol.append(key)
# RETURN
return sol