Skip to content

Commit f51f720

Browse files
committed
501
1 parent 6f67dfd commit f51f720

File tree

2 files changed

+51
-1
lines changed

2 files changed

+51
-1
lines changed

README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ Success is like pregnancy, Everybody congratulates you but nobody knows how many
7171
|110|[Balanced Binary Tree](https://leetcode.com/problems/balanced-binary-tree/#/description)| [Python [Yu]](./tree/Yu/110_isBalanced.py) | _O(N)_| _O(h)_ | Easy | ||
7272
|112|[Path Sum](https://leetcode.com/problems/path-sum/#/description)| [Python [Yu]](./tree/Yu/112_hasPathSum.py) | _O(N)_| _O(h)_ | Easy | |[中文讲解](https://www.youtube.com/watch?v=LgtcGjIuE18&feature=youtu.be)|
7373
|404|[Sum of Left Leaves](https://leetcode.com/problems/sum-of-left-leaves/#/description)| [Python [Yu]](./tree/Yu/404_sum_of_Left_Leaves.py) | _O(N)_| _O(h)_ | Easy | ||
74-
|543|[Diameter of Binary Tree](https://leetcode.com/problems/diameter-of-binary-tree/#/description)| [Python [Yu]](./tree/Yu/543.py) | _O(N)_| _O(h)_ | Easy | |[公瑾讲解](https://www.youtube.com/watch?v=0VnOfu2pYTo)|
74+
7575

7676

7777

@@ -90,6 +90,9 @@ Success is like pregnancy, Everybody congratulates you but nobody knows how many
9090
|-----|-------| -------- | ---- | ------|------------|---|-----|
9191
|108|[Convert Sorted Array to Binary Search Tree](https://leetcode.com/problems/convert-sorted-array-to-binary-search-tree/#/solutions)| [Python [Yu]](./tree/Yu/108.py) | _O(N)_| _O(N)_ | Easy | |[公瑾讲解](https://www.youtube.com/watch?v=lBrb4fXPcMM)|
9292
|226|[Invert Binary Tree](https://leetcode.com/problems/invert-binary-tree/#/description)| [Python [Yu]](./tree/Yu/226.py) | _O(N)_| _O(1)_ | Easy | |[公瑾讲解](https://youtu.be/oiX3mqcAK0s)|
93+
|543|[Diameter of Binary Tree](https://leetcode.com/problems/diameter-of-binary-tree/#/description)| [Python [Yu]](./tree/Yu/543.py) | _O(N)_| _O(h)_ | Easy | |[公瑾讲解](https://www.youtube.com/watch?v=0VnOfu2pYTo)|
94+
|501|[Find Mode in Binary Search Tree](https://leetcode.com/problems/find-mode-in-binary-search-tree/#/description)| [Python [Yu]](./tree/Yu/501.py) | _O(N)_| _O(N)_ | Easy | |[公瑾讲解](https://youtu.be/v4F4x_uwMb8)|
95+
9396
|563|[Binary Tree Tilt](https://leetcode.com/problems/binary-tree-tilt/#/description)| [Python [Yu]](./tree/Yu/563.py) | _O(N)_| _O(1)_ | Easy | |[公瑾讲解](https://youtu.be/47FQVP4ynk0)|
9497
|257|[Binary Tree Paths](https://leetcode.com/problems/binary-tree-paths/#/description)| [Python [Yu]](./tree/Yu/257.py) | _O(N)_| _O(N)_ | Easy | |[公瑾讲解](https://youtu.be/Zr_7qq2f16k)|
9598
|102|[Binary Tree Level Order Traversal](https://leetcode.com/problems/binary-tree-level-order-traversal/#/description)| [Python [Yu]](./tree/Yu/102.py) | _O(N)_| _O(N)_ | Medium | |[公瑾讲解](https://youtu.be/IWiprpdSgzg)|

tree/Yu/501.py

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
#!/usr/bin/python
2+
# -*- coding: utf-8 -*-
3+
4+
# Author: Yu Zhou
5+
# 501. Find Mode in Binary Search Tree
6+
# ****************
7+
# Descrption:
8+
# Given a binary search tree (BST) with duplicates, find all the mode(s)
9+
# (the most frequently occurred element) in the given BST.
10+
# ****************
11+
12+
class Solution(object):
13+
def findMode(self, root):
14+
"""
15+
:type root: TreeNode
16+
:rtype: List[int]
17+
"""
18+
19+
def find_hash(root, hash):
20+
# Edge:
21+
if not root:
22+
return
23+
24+
# Process
25+
if root.val in hash:
26+
hash[root.val] += 1
27+
else:
28+
hash[root.val] = 1
29+
30+
# Recursion
31+
find_hash(root.left, hash)
32+
find_hash(root.right, hash)
33+
34+
# Edge
35+
if not root:
36+
return []
37+
38+
hash = {}
39+
res = []
40+
find_hash(root, hash)
41+
42+
max_value = max(hash.values())
43+
44+
for key in hash.keys():
45+
if hash[key] == max_value:
46+
res.append(key)
47+
return res

0 commit comments

Comments
 (0)