当前位置:网站首页>【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
}
边栏推荐
- Vulnstack3
- Numpy library introductory tutorial: basic knowledge summary
- openresty ngx_lua執行階段
- Redis distributed lock, lock code logic
- "C zero foundation introduction hundred knowledge and hundred cases" (72) multi wave entrustment -- Mom shouted for dinner
- 如何搭建一支搞垮公司的技術團隊?
- RichView TRVStyle MainRVStyle
- Traditional chips and AI chips
- "2022" is a must know web security interview question for job hopping
- How to build a technical team that will bring down the company?
猜你喜欢
如何做一个炫酷的墨水屏电子钟?
JVM - when multiple threads initialize the same class, only one thread is allowed to initialize
Application and Optimization Practice of redis in vivo push platform
The perfect car for successful people: BMW X7! Superior performance, excellent comfort and safety
CAM Pytorch
Security level
丸子百度小程序详细配置教程,审核通过。
One plus six brushes into Kali nethunter
Missile interception -- UPC winter vacation training match
Practice of tdengine in TCL air conditioning energy management platform
随机推荐
The most powerful new household god card of Bank of communications. Apply to earn 2100 yuan. Hurry up if you haven't applied!
Do you know the eight signs of a team becoming agile?
ICSI 311 Parser
Win: add general users to the local admins group
RichView TRVUnits 图像显示单位
spoon插入更新oracle数据库,插了一部分提示报错Assertion botch: negative time
Li Kou Jianzhi offer -- binary tree chapter
Video display and hiding of imitation tudou.com
Last week's hot review (2.7-2.13)
[understanding of opportunity -38]: Guiguzi - Chapter 5 flying clamp - warning one: there is a kind of killing called "killing"
How to find hot projects in 2022? Dena community project progress follow-up, there is always a dish for you (1)
[technology development-26]: data security of new information and communication networks
When to catch an exception and when to throw an exception- When to catch the Exception vs When to throw the Exceptions?
The application and Optimization Practice of redis in vivo push platform is transferred to the end of metadata by
The steering wheel can be turned for one and a half turns. Is there any difference between it and two turns
Go RPC call
Chinese natural language processing, medical, legal and other public data sets, sorting and sharing
Open source SPL optimized report application coping endlessly
Codeforces Global Round 19 ABC
What is the length of SHA512 hash string- What is the length of a hashed string with SHA512?