当前位置:网站首页>22 -- range and of binary search tree
22 -- range and of binary search tree
2022-07-24 02:30:00 【JH_ Cao】
1. subject
- The title means :
Traverse all nodes , As long as the value of the node is within the range , Just sum , If it's not in range , Don't count in
2. Ideas
- Understand the meaning of the topic , It's easy to ask .
- adopt DFS Ergodic summation .
var sum = 0
func rangeSumBST(_ root: TreeNode?, _ low: Int, _ high: Int) -> Int {
dfs(root: root, low: low, high: high)
return sum
}
func dfs(root: TreeNode?, low: Int, high: Int) {
if root == nil {
return
}
if root!.val >= low && root!.val <= high {
sum += root!.val
}
dfs(root: root?.left, low: low, high: high)
dfs(root: root?.right, low: low, high: high)
}
- Train of thought two
BFS
But the efficiency is much slower
Code :var sum = 0 var queue: [TreeNode] = [TreeNode]() func rangeSumBST(_ root: TreeNode?, _ low: Int, _ high: Int) -> Int { if root == nil { return sum } bfs(root: root, low: low, high: high) return sum } func bfs(root: TreeNode?, low: Int, high: Int) { if root != nil { queue.append(root!) } while !queue.isEmpty { let node = queue.removeFirst() let value = node.val if value >= low && value <= high { sum += value } if node.left != nil { queue.append(node.left!) } if node.right != nil { queue.append(node.right!) } } }
3. Submit
BFS: 2504ms
DFS: 420 ms
边栏推荐
- [Luogu] p1318 ponding area
- Use of component El scrollbar
- 认识传输层协议—TCP/UDP
- Use the pagoda panel to plan tasks and automatically push the website to Baidu for inclusion
- Network protocol details: TCP part1
- 1000个Okaleido Tiger首发上线Binance NFT,引发抢购热潮
- 【补题日记】[2022牛客暑期多校1]J-Serval and Essay
- After five years of contact with nearly 100 bosses, as a headhunter, I found that the secret of promotion was only four words
- redis数据类型概念
- 餐饮连锁门店重塑增长背后的数字化转型
猜你喜欢

IBM: realize the quantum advantage of fault tolerance by 2030

Writing of graph nodes that trigger different special effects during the day and at night in Tiktok

pbootcms模板调用标签序数从2开始或者自动数开始

1000个Okaleido Tiger首发上线Binance NFT,引发抢购热潮

【数据集】——flyingthings3d光流部分数据集下载

Resumption: a deck of cards (54), three people fighting the landlord, what is the probability that the big and small kings are in the same family
![[C language] preprocessing details](/img/c3/861165ce20c135f4feedee1f112261.png)
[C language] preprocessing details

Network protocol details: TCP part1

Wechat applet setting background image does not display problem solution

响应式pbootcms模板装修设计类网站
随机推荐
关于 SAP Fiori 应用的离线使用
Network protocol details: UDP
MySQL---four JDBC
Causal learning open source project: from prediction to decision!
Opensmile introduction and problems encountered during installation
Wechat applet setting background image does not display problem solution
Halide:: generator instructions
毕业设计校园信息发布平台网站源码
Resumption: a deck of cards (54), three people fighting the landlord, what is the probability that the big and small kings are in the same family
什么叫裸写SQL?express操作mysql用什么中件间或插件好呢?
网络协议详解 :UDP
输入cnpm -v出现cnpm : 无法加载文件 C:\Users\19457\AppData\Roaming\npm\cnpm.ps1,因为在此系统上禁止运行脚本。
Jina AI and datawhale jointly launched a learning project!
Redraw the button and make your own circular LED indicator
[untitled]
7 issues to consider before website construction
[diary of supplementary questions] [2022 Niuke summer school 1] i-chiitoitsu
Chinese scientists have made new progress in high security quantum key distribution networks
MySQL---four JDBC
Loadrunner12 installation, recording the first script and the proxy server did not respond to the solution
