Skip to content

Commit 5ea4754

Browse files
committed
Time: 60 ms (36.02%), Space: 20.7 MB (56.06%) - LeetHub
1 parent e5b47c4 commit 5ea4754

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# Definition for a binary tree node.
2+
# class TreeNode:
3+
# def __init__(self, val=0, left=None, right=None):
4+
# self.val = val
5+
# self.left = left
6+
# self.right = right
7+
class Solution:
8+
def findMode(self, root: Optional[TreeNode]) -> List[int]:
9+
l=[]
10+
def inorder(root) :
11+
if not root :
12+
return
13+
if root.left :
14+
inorder(root.left)
15+
l.append(root.val)
16+
if root.right :
17+
inorder(root.right)
18+
inorder(root)
19+
d={}
20+
for x in l :
21+
d[x]=d.get(x,0)+1
22+
m,v=0,0
23+
for x in d :
24+
if d[x] > m :
25+
m=d[x]
26+
ans=[]
27+
for x in d :
28+
if d[x]==m :
29+
ans.append(x)
30+
return ans

0 commit comments

Comments
 (0)