Movatterモバイル変換


[0]ホーム

URL:


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

Commit7bbc8ba

Browse files
Merge pull requestyoungyangyang04#406 from X-shuffle/master
添加0071.0501.二叉搜索树中的众数 go版本
2 parentse332190 +2f514cd commit7bbc8ba

File tree

2 files changed

+141
-0
lines changed

2 files changed

+141
-0
lines changed

‎problems/0235.二叉搜索树的最近公共祖先.md

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,54 @@ class Solution:
265265
else:return root
266266
```
267267
Go:
268+
> BSL法
269+
270+
```go
271+
/**
272+
* Definition for a binary tree node.
273+
* type TreeNode struct {
274+
* Val int
275+
* Left *TreeNode
276+
* Right *TreeNode
277+
* }
278+
*/
279+
//利用BSL的性质(前序遍历有序)
280+
funclowestCommonAncestor(root, p, q*TreeNode)*TreeNode {
281+
if root==nil{return nil}
282+
if root.Val>p.Val&&root.Val>q.Val{//当前节点的值大于给定的值,则说明满足条件的在左边
283+
return lowestCommonAncestor(root.Left,p,q)
284+
}else if root.Val<p.Val&&root.Val<q.Val{//当前节点的值小于各点的值,则说明满足条件的在右边
285+
return lowestCommonAncestor(root.Right,p,q)
286+
}else {return root}//当前节点的值在给定值的中间(或者等于),即为最深的祖先
287+
}
288+
```
289+
290+
> 普通法
291+
292+
```go
293+
/**
294+
* Definition for a binary tree node.
295+
* type TreeNode struct {
296+
* Val int
297+
* Left *TreeNode
298+
* Right *TreeNode
299+
* }
300+
*/
301+
//递归会将值层层返回
302+
func lowestCommonAncestor(root, p, q *TreeNode) *TreeNode {
303+
//终止条件
304+
if root==nil||root.Val==p.Val||root.Val==q.Val{return root}//最后为空或者找到一个值时,就返回这个值
305+
//后序遍历
306+
findLeft:=lowestCommonAncestor(root.Left,p,q)
307+
findRight:=lowestCommonAncestor(root.Right,p,q)
308+
//处理单层逻辑
309+
if findLeft!=nil&&findRight!=nil{return root}//说明在root节点的两边
310+
if findLeft==nil{//左边没找到,就说明在右边找到了
311+
return findRight
312+
}else {return findLeft}
313+
}
314+
```
315+
268316

269317

270318

‎problems/0501.二叉搜索树中的众数.md

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -428,7 +428,100 @@ class Solution:
428428
return self.res
429429
```
430430
Go:
431+
暴力法(非BSL)
432+
433+
```go
434+
/**
435+
* Definition for a binary tree node.
436+
* type TreeNode struct {
437+
* Val int
438+
* Left *TreeNode
439+
* Right *TreeNode
440+
* }
441+
*/
442+
funcfindMode(root *TreeNode) []int {
443+
varhistorymap[int]int
444+
varmaxValueint
445+
varmaxIndexint
446+
varresult []int
447+
history=make(map[int]int)
448+
traversal(root,history)
449+
for k,value:=range history{
450+
if value>maxValue{
451+
maxValue=value
452+
maxIndex=k
453+
}
454+
}
455+
for k,value:=range history{
456+
if value==history[maxIndex]{
457+
result=append(result,k)
458+
}
459+
}
460+
return result
461+
}
462+
functraversal(root *TreeNode,historymap[int]int){
463+
if root.Left!=nil{
464+
traversal(root.Left,history)
465+
}
466+
if value,ok:=history[root.Val];ok{
467+
history[root.Val]=value+1
468+
}else{
469+
history[root.Val]=1
470+
}
471+
if root.Right!=nil{
472+
traversal(root.Right,history)
473+
}
474+
}
475+
```
431476

477+
计数法BSL(此代码在执行代码里能执行,但提交后报错,不知为何,思路是对的)
478+
479+
```go
480+
/**
481+
* Definition for a binary tree node.
482+
* type TreeNode struct {
483+
* Val int
484+
* Left *TreeNode
485+
* Right *TreeNode
486+
* }
487+
*/
488+
varcount,maxCountint//统计计数
489+
funcfindMode(root *TreeNode) []int {
490+
varresult []int
491+
varpre *TreeNode//前指针
492+
if root.Left==nil&&root.Right==nil{
493+
result=append(result,root.Val)
494+
return result
495+
}
496+
traversal(root,&result,pre)
497+
return result
498+
}
499+
functraversal(root *TreeNode,result *[]int,pre *TreeNode){//遍历统计
500+
//如果BSL中序遍历相邻的两个节点值相同,则统计频率;如果不相同,依据BSL中序遍历排好序的性质,重新计数
501+
if pre==nil{
502+
count=1
503+
}elseif pre.Val==root.Val{
504+
count++
505+
}else {
506+
count=1
507+
}
508+
//如果统计的频率等于最大频率,则加入结果集;如果统计的频率大于最大频率,更新最大频率且重新将结果加入新的结果集中
509+
if count==maxCount{
510+
*result=append(*result,root.Val)
511+
}elseif count>maxCount{
512+
maxCount=count//重新赋值maxCount
513+
*result=[]int{}//清空result中的内容
514+
*result=append(*result,root.Val)
515+
}
516+
pre=root//保存上一个的节点
517+
if root.Left!=nil{
518+
traversal(root.Left,result,pre)
519+
}
520+
if root.Right!=nil{
521+
traversal(root.Right,result,pre)
522+
}
523+
}
524+
```
432525

433526

434527

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp