当前位置:网站首页>【LeetCode】501. Mode in binary search tree (2 wrong questions)
【LeetCode】501. Mode in binary search tree (2 wrong questions)
2022-07-05 02:20:00 【Kaimar】
- Ideas
Simple ideas , Traverse in middle order and save the number , Find the maximum number of occurrences , Finally, list the number according to the number of occurrences .
Using the property of order traversal in binary search tree , Middle order traversal is ordered , So don't save it , You only need to record the number of occurrences and the corresponding value .
/** * Definition for a binary tree node. * type TreeNode struct { * Val int * Left *TreeNode * Right *TreeNode * } */
func findMode(root *TreeNode) []int {
res := []int{
}
var count int
var maxCount int
var preNode *TreeNode
var inOrder func(node *TreeNode)
inOrder = func(node *TreeNode) {
if node == nil {
return
}
// Left
inOrder(node.Left)
// in
if preNode == nil {
count = 1
} else if preNode.Val == node.Val {
count++
} else {
count = 1
}
if count > maxCount {
maxCount = count
res = []int{
node.Val}
} else if count == maxCount {
res = append(res, node.Val)
}
preNode = node
// Right
inOrder(node.Right)
}
inOrder(root)
return res
}
边栏推荐
- Runc hang causes the kubernetes node notready
- Icu4c 70 source code download and compilation (win10, vs2022)
- Application and development trend of image recognition technology
- Vulnstack3
- [技术发展-26]:新型信息与通信网络的数据安全
- LeetCode 314. Binary tree vertical order traversal - Binary Tree Series Question 6
- Yyds dry inventory jetpack hit dependency injection framework Getting Started Guide
- Pgadmin 4 V6.5 release, PostgreSQL open source graphical management tool
- 如何搭建一支搞垮公司的技術團隊?
- Interesting practice of robot programming 15- autoavoidobstacles
猜你喜欢
Exploration of short text analysis in the field of medical and health (II)
One plus six brushes into Kali nethunter
Marubeni Baidu applet detailed configuration tutorial, approved.
Security level
Practical case of SQL optimization: speed up your database
The perfect car for successful people: BMW X7! Superior performance, excellent comfort and safety
Variables in postman
Unpool(nn.MaxUnpool2d)
. Net starts again happy 20th birthday
JVM's responsibility - load and run bytecode
随机推荐
Why do you understand a16z? Those who prefer Web3.0 Privacy Infrastructure: nym
Mysql database | build master-slave instances of mysql-8.0 or above based on docker
官宣!第三届云原生编程挑战赛正式启动!
How to make a cool ink screen electronic clock?
Five ways to query MySQL field comments!
Redis' hyperloglog as a powerful tool for active user statistics
PowerShell:在代理服务器后面使用 PowerShell
Learn tla+ (XII) -- functions through examples
Interpretation of mask RCNN paper
低度酒赛道进入洗牌期,新品牌如何破局三大难题?
CAM Pytorch
spoon插入更新oracle数据库,插了一部分提示报错Assertion botch: negative time
Binary tree traversal - middle order traversal (golang)
openresty ngx_lua執行階段
Interesting practice of robot programming 14 robot 3D simulation (gazebo+turtlebot3)
Li Kou Jianzhi offer -- binary tree chapter
Practical case of SQL optimization: speed up your database
A label making navigation bar
Naacl 2021 | contrastive learning sweeping text clustering task
[uc/os-iii] chapter 1.2.3.4 understanding RTOS