当前位置:网站首页>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
边栏推荐
- IBM: realize the quantum advantage of fault tolerance by 2030
- [untitled]
- Running around, market and quantitative page function optimization! Stock quantitative analysis tool qtyx-v2.4.5
- Responsive pbootcms template decoration design website
- Jina AI and datawhale jointly launched a learning project!
- The combination sum of C language power deduction question 39. Backtracking method and traversal method
- Mysql数据库,排序与单行处理函数篇
- 网络协议详解 :UDP
- Tensorflow 2.0 deep learning tutorial
- 7 issues to consider before website construction
猜你喜欢

wallys/WiFi6 MiniPCIe Module 2T2R2 × 2.4GHz 2x5GHz MT7915 MT7975

Use of component El scrollbar

5年接触近百位老板,身为猎头的我,发现升职的秘密不过4个字

College degree want to 0 basic programming after looking for a job feasible?

WordPress website SEO complete tutorial

Leetcode 203. remove linked list elements (2022.07.22)

氢能创业大赛 | 国华投资董事长刘小奇:发挥风光氢储融一体化优势 高水平承办创业大赛

认识传输层协议—TCP/UDP

我国科学家在高安全量子密钥分发网络方面取得新进展

Visual full link log tracking
随机推荐
Halide:: generator instructions
BPG notes (III)
Use the pagoda panel to plan tasks and automatically push the website to Baidu for inclusion
Loadrunner12 installation, recording the first script and the proxy server did not respond to the solution
使用第三方账号登录
中城院真的在帮助供应商解决问题吗?
Seatunnel architecture
WordPress website SEO complete tutorial
The combination sum of C language power deduction question 39. Backtracking method and traversal method
Network protocol details: UDP
STM32 installation tutorial and j-link burning driver installation tutorial [the next day]
Jina AI 联合Datawhale,发起学习项目!
Camper recruitment | AI youth with the world in mind, the United Nations needs your help for sustainable development!
How to judge null for different types of fields, sets, lists / sets / maps, and objects
NetApp FAS系列一个CIFS bug引起的控制器重启案例分享
Understand the transport layer protocol - tcp/udp
[knowledge atlas] practice -- Practice of question and answer system based on medical knowledge atlas (Part2): Atlas data preparation and import
Mysql数据库,排序与单行处理函数篇
【知识图谱】实践篇——基于医疗知识图谱的问答系统实践(Part2):图谱数据准备与导入
Preliminary use of 145 keep alive
