# 一、题目

• 结点左子树中所含结点的值小于等于当前结点的值
• 结点右子树中所含结点的值大于等于当前结点的值
• 左子树和右子树都是二叉搜索树

1
\
2
/
2

# 三、思路

1、中序遍历+有序数组的众数

2、遍历（先序，中序，后序均可）+dict字典

3、中序遍历

now记录上一个结点，now[0]为结点的val值，now[1]为该val值出现的次数；

max_val记录当前出现次数最大的值与次数，max_val[0]为次数最大值的val值，max_val[1]为次数最大值的次数；

# 四、代码

1、

# Definition for a binary tree node.
class TreeNode:
def __init__(self, x):
self.val = x
self.left = None
self.right = None

class Solution:
def findMode(self, root: TreeNode):
# 中序遍历 + 有序数组求众数
res = []
def InTraverse(root):
if root.left:
InTraverse(root.left)
res.append(root.val)
if root.right:
InTraverse(root.right)
if root:
InTraverse(root)
# print(res)

ans = []
count = 0
max_count = 0
last = None

# if len(res) == 0:
#     return []

for num in res:
if last == num:
count += 1
else:
count = 1
if count > max_count:
ans = [num]
max_count = count
elif count == max_count:
ans.append(num)
last = num
return ans

if __name__ == '__main__':
# root = TreeNode(1)
# root.right = TreeNode(2)
# root.right.left = TreeNode(2)
# root.right.right = TreeNode(2)

root = TreeNode(1)
root.left = TreeNode(1)
root.right = TreeNode(2)
s = Solution()
ans = s.findMode(root)
print(ans)

2、

# Definition for a binary tree node.
class TreeNode:
def __init__(self, x):
self.val = x
self.left = None
self.right = None

class Solution:
def findMode(self, root: TreeNode):
if root is None:
return []
dict = {}
res = []

def DFS(root):
if root is None:
return
if root.val not in dict:
dict[root.val] = 1
else:
dict[root.val] += 1
DFS(root.left)
DFS(root.right)
DFS(root)
# print(dict)
max_val = max(dict.values())
# print(max_val)
for item, val in dict.items():
if val == max_val:
res.append(item)
return res

if __name__ == '__main__':
# root = TreeNode(1)
# root.right = TreeNode(2)
# root.right.left = TreeNode(2)
# root.right.right = TreeNode(2)

root = TreeNode(1)
root.left = TreeNode(1)
root.right = TreeNode(2)
s = Solution()
ans = s.findMode(root)
print(ans)

3、

# Definition for a binary tree node.
class TreeNode:
def __init__(self, x):
self.val = x
self.left = None
self.right = None

class Solution:
def findMode(self, root: TreeNode):
now = [0, 0]
max_val = [0, 0]
res = []

def InTraverse(root):
nonlocal now, max_val, res

if root.left:
InTraverse(root.left)

if root.val == now[0]:
now[1] += 1
else:
now[0] = root.val
now[1] = 1

if now[1] > max_val[1]:
max_val[1] = now[1]
max_val[0] = now[0]
res = [max_val[0]]
elif now[1] == max_val[1]:
res.append(now[0])

if root.right:
InTraverse(root.right)

if root:
InTraverse(root)
return res

if __name__ == '__main__':
# root = TreeNode(1)
# root.right = TreeNode(2)
# root.right.left = TreeNode(2)
# root.right.right = TreeNode(2)

root = TreeNode(1)
root.left = TreeNode(1)
root.right = TreeNode(2)
s = Solution()
ans = s.findMode(root)
print(ans)

• 点赞
• 评论
• 分享
x

海报分享

扫一扫，分享海报

• 收藏
• 手机看

分享到微信朋友圈

x

扫一扫，手机阅读

• 打赏

打赏

D77_d77

你的鼓励将是我创作的最大动力

C币 余额
2C币 4C币 6C币 10C币 20C币 50C币
• 一键三连

点赞Mark关注该博主, 随时了解TA的最新博文
11-21 128

06-06 525
02-21 365
02-03 155
01-31 163
08-08 1041
©️2020 CSDN 皮肤主题: 深蓝海洋 设计师:CSDN官方博客