mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-11 21:10:58 +08:00
Update 0501.二叉搜索树中的众数.md
This commit is contained in:
@ -475,8 +475,7 @@ class Solution {
|
||||
|
||||
## Python
|
||||
|
||||
> 递归法
|
||||
> 常量空间,递归产生的栈不算
|
||||
递归法(版本一)利用字典
|
||||
|
||||
```python
|
||||
# Definition for a binary tree node.
|
||||
@ -485,77 +484,108 @@ class Solution {
|
||||
# self.val = val
|
||||
# self.left = left
|
||||
# self.right = right
|
||||
from collections import defaultdict
|
||||
|
||||
class Solution:
|
||||
def __init__(self):
|
||||
self.pre = TreeNode()
|
||||
self.count = 0
|
||||
self.max_count = 0
|
||||
self.result = []
|
||||
def searchBST(self, cur, freq_map):
|
||||
if cur is None:
|
||||
return
|
||||
freq_map[cur.val] += 1 # 统计元素频率
|
||||
self.searchBST(cur.left, freq_map)
|
||||
self.searchBST(cur.right, freq_map)
|
||||
|
||||
def findMode(self, root: TreeNode) -> List[int]:
|
||||
if not root: return None
|
||||
self.search_BST(root)
|
||||
return self.result
|
||||
def findMode(self, root):
|
||||
freq_map = defaultdict(int) # key:元素,value:出现频率
|
||||
result = []
|
||||
if root is None:
|
||||
return result
|
||||
self.searchBST(root, freq_map)
|
||||
max_freq = max(freq_map.values())
|
||||
for key, freq in freq_map.items():
|
||||
if freq == max_freq:
|
||||
result.append(key)
|
||||
return result
|
||||
|
||||
def search_BST(self, cur: TreeNode) -> None:
|
||||
if not cur: return None
|
||||
self.search_BST(cur.left)
|
||||
# 第一个节点
|
||||
if not self.pre:
|
||||
self.count = 1
|
||||
# 与前一个节点数值相同
|
||||
elif self.pre.val == cur.val:
|
||||
self.count += 1
|
||||
# 与前一个节点数值不相同
|
||||
else:
|
||||
self.count = 1
|
||||
self.pre = cur
|
||||
|
||||
if self.count == self.max_count:
|
||||
self.result.append(cur.val)
|
||||
|
||||
if self.count > self.max_count:
|
||||
self.max_count = self.count
|
||||
self.result = [cur.val] # 清空self.result,确保result之前的的元素都失效
|
||||
|
||||
self.search_BST(cur.right)
|
||||
```
|
||||
|
||||
递归法(版本二)利用二叉搜索树性质
|
||||
|
||||
> 迭代法-中序遍历
|
||||
> 利用二叉搜索树特性,在历遍过程中更新结果,一次历遍
|
||||
> 但需要使用额外空间存储历遍的节点
|
||||
```python
|
||||
class Solution:
|
||||
def findMode(self, root: TreeNode) -> List[int]:
|
||||
stack = []
|
||||
def __init__(self):
|
||||
self.maxCount = 0 # 最大频率
|
||||
self.count = 0 # 统计频率
|
||||
self.pre = None
|
||||
self.result = []
|
||||
|
||||
def searchBST(self, cur):
|
||||
if cur is None:
|
||||
return
|
||||
|
||||
self.searchBST(cur.left) # 左
|
||||
# 中
|
||||
if self.pre is None: # 第一个节点
|
||||
self.count = 1
|
||||
elif self.pre.val == cur.val: # 与前一个节点数值相同
|
||||
self.count += 1
|
||||
else: # 与前一个节点数值不同
|
||||
self.count = 1
|
||||
self.pre = cur # 更新上一个节点
|
||||
|
||||
if self.count == self.maxCount: # 如果与最大值频率相同,放进result中
|
||||
self.result.append(cur.val)
|
||||
|
||||
if self.count > self.maxCount: # 如果计数大于最大值频率
|
||||
self.maxCount = self.count # 更新最大频率
|
||||
self.result = [cur.val] # 很关键的一步,不要忘记清空result,之前result里的元素都失效了
|
||||
|
||||
self.searchBST(cur.right) # 右
|
||||
return
|
||||
|
||||
def findMode(self, root):
|
||||
self.count = 0
|
||||
self.maxCount = 0
|
||||
self.pre = None # 记录前一个节点
|
||||
self.result = []
|
||||
|
||||
self.searchBST(root)
|
||||
return self.result
|
||||
```
|
||||
迭代法
|
||||
```python
|
||||
class Solution:
|
||||
def findMode(self, root):
|
||||
st = []
|
||||
cur = root
|
||||
pre = None
|
||||
maxCount, count = 0, 0
|
||||
res = []
|
||||
while cur or stack:
|
||||
if cur: # 指针来访问节点,访问到最底层
|
||||
stack.append(cur)
|
||||
cur = cur.left
|
||||
else: # 逐一处理节点
|
||||
cur = stack.pop()
|
||||
if pre == None: # 第一个节点
|
||||
maxCount = 0 # 最大频率
|
||||
count = 0 # 统计频率
|
||||
result = []
|
||||
|
||||
while cur is not None or st:
|
||||
if cur is not None: # 指针来访问节点,访问到最底层
|
||||
st.append(cur) # 将访问的节点放进栈
|
||||
cur = cur.left # 左
|
||||
else:
|
||||
cur = st.pop()
|
||||
if pre is None: # 第一个节点
|
||||
count = 1
|
||||
elif pre.val == cur.val: # 与前一个节点数值相同
|
||||
count += 1
|
||||
else:
|
||||
else: # 与前一个节点数值不同
|
||||
count = 1
|
||||
if count == maxCount:
|
||||
res.append(cur.val)
|
||||
if count > maxCount:
|
||||
maxCount = count
|
||||
res.clear()
|
||||
res.append(cur.val)
|
||||
|
||||
if count == maxCount: # 如果和最大值相同,放进result中
|
||||
result.append(cur.val)
|
||||
|
||||
if count > maxCount: # 如果计数大于最大值频率
|
||||
maxCount = count # 更新最大频率
|
||||
result = [cur.val] # 很关键的一步,不要忘记清空result,之前result里的元素都失效了
|
||||
|
||||
pre = cur
|
||||
cur = cur.right
|
||||
return res
|
||||
cur = cur.right # 右
|
||||
|
||||
return result
|
||||
```
|
||||
## Go
|
||||
|
||||
|
Reference in New Issue
Block a user