Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit e474f16

Browse files
add LeetCode 501. 二叉搜索树中的众数
1 parent 448ccf4 commit e474f16

File tree

1 file changed

+112
-0
lines changed

1 file changed

+112
-0
lines changed
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
![](https://imgconvert.csdnimg.cn/aHR0cHM6Ly9jZG4uanNkZWxpdnIubmV0L2doL2Nob2NvbGF0ZTE5OTkvY2RuL2ltZy8yMDIwMDgyODE0NTUyMS5qcGc?x-oss-process=image/format,png)
2+
>仰望星空的人,不应该被嘲笑
3+
4+
## 题目描述
5+
给定一个有相同值的二叉搜索树(**BST**),找出 **BST** 中的所有众数(出现频率最高的元素)。
6+
7+
假定 BST 有如下定义:
8+
9+
- 结点左子树中所含结点的值小于等于当前结点的值
10+
- 结点右子树中所含结点的值大于等于当前结点的值
11+
- 左子树和右子树都是二叉搜索树
12+
13+
例如:
14+
15+
```javascript
16+
给定 BST [1,null,2,2],
17+
18+
1
19+
\
20+
2
21+
/
22+
2
23+
返回[2].
24+
```
25+
26+
提示:如果众数超过1个,不需考虑输出顺序
27+
28+
进阶:你可以不使用额外的空间吗?(假设由递归产生的隐式调用栈的开销不被计算在内)
29+
30+
来源:力扣(LeetCode)
31+
链接:https://leetcode-cn.com/problems/find-mode-in-binary-search-tree
32+
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
33+
34+
35+
36+
37+
## 解题思路
38+
39+
由于 `BST`(二叉搜索树)的特殊性,我们采用递归来中序遍历,访问的节点值是有序的。然后重复节点,用计数器进行累加即可,如果有新值出现,则更新新值,然后计数器重置为 1。然后对于当前次数超过了最大值,则更新当前最大值,如果等于最大值,则代表出现了相同频率的数字,加入即可。
40+
41+
如果次数小于最大值,不需要什么操作。
42+
43+
```javascript
44+
/**
45+
* Definition for a binary tree node.
46+
* function TreeNode(val) {
47+
* this.val = val;
48+
* this.left = this.right = null;
49+
* }
50+
*/
51+
/**
52+
* @param {TreeNode} root
53+
* @return {number[]}
54+
*/
55+
var findMode = function(root) {
56+
let cnt = 0;
57+
let pre = 0;
58+
let res = [];
59+
let maxCnt = 0;
60+
let handle = (cur) => {
61+
// 相同的数,累加
62+
if(cur === pre){
63+
cnt++;
64+
}else{
65+
// 有新数出现,重新置计数器为1,更新新数
66+
pre = cur;
67+
cnt = 1;
68+
}
69+
// 如果次数超过了最大值,更新当前最大值
70+
if(cnt > maxCnt){
71+
maxCnt = cnt;
72+
res = [cur];
73+
// 如果有相同频率的数字出现,直接加入
74+
}else if(cnt === maxCnt){
75+
res.push(cur);
76+
}
77+
}
78+
// 二叉搜索树,递归中序遍历方式
79+
let inOrder = (root) =>{
80+
if(!root) return null;
81+
inOrder(root.left);
82+
handle(root.val);
83+
inOrder(root.right);
84+
}
85+
inOrder(root);
86+
return res;
87+
};
88+
```
89+
90+
91+
92+
## 最后
93+
文章产出不易,还望各位小伙伴们支持一波!
94+
95+
往期精选:
96+
97+
<a href="https://github.com/Chocolate1999/Front-end-learning-to-organize-notes">小狮子前端の笔记仓库</a>
98+
99+
<a href="https://github.com/Chocolate1999/leetcode-javascript">leetcode-javascript:LeetCode 力扣的 JavaScript 解题仓库,前端刷题路线(思维导图)</a>
100+
101+
小伙伴们可以在Issues中提交自己的解题代码,🤝 欢迎Contributing,可打卡刷题,Give a ⭐️ if this project helped you!
102+
103+
104+
<a href="https://yangchaoyi.vip/">访问超逸の博客</a>,方便小伙伴阅读玩耍~
105+
106+
![](https://img-blog.csdnimg.cn/2020090211491121.png#pic_center)
107+
108+
```javascript
109+
学如逆水行舟,不进则退
110+
```
111+
112+

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /