Easy
使用最简单粗暴的方式求解:
以上,尝试写一下代码,AC!
python3
class Solution:
def findMode(self, root: TreeNode) -> List[int]:
counter = collections.defaultdict(int)
def dfs(node):
if node is None:
return
counter[node.val] += 1
dfs(node.left)
dfs(node.right)
return
dfs(root)
sorted_counter = sorted(counter.items(), key = lambda kv: kv[1], reverse = True)
pre_c = 0
res = []
for i in sorted_counter:
if i[1] >= pre_c:
res.append(i[0])
pre_c = i[1]
else:
break
return res