当前位置:网站首页>22 -- 二叉搜索树的范围和
22 -- 二叉搜索树的范围和
2022-07-24 02:25:00 【JH_Cao】
1. 题目
- 题目的意思就是说:
遍历所有的节点,只要节点的值在范围内,就求和,如果不在范围内,就不算进来
2. 思路
- 理解了题目的意思,就容易求了。
- 通过DFS遍历求和。
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)
}
- 思路二
BFS
但是效率慢很多
代码: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. 提交
BFS: 2504ms
DFS: 420 ms
边栏推荐
- POP3客户端代码的实现
- LoadRunner12安装、录制第一个脚本以及代理服务器没有响应解决
- ggplot2显示png
- WordPress website SEO complete tutorial
- Build a CPU Simulator
- [重要通知]星球线上培训第三期来袭!讲解如何在QTYX上构建自己的量化策略!...
- Small volume stock trading record | based on multi task crawler technology, realize level1 sampling of A-share real-time market
- Redraw the button and make your own circular LED indicator
- Leetcode 203. remove linked list elements (2022.07.22)
- 浅谈领域驱动设计
猜你喜欢

Opensmile introduction and problems encountered during installation

Research on XMPP service (I)

The communication principle between native components, applets and clients, and the operation principle of video, map, canvas, picker, etc

In depth understanding - wechat developer tools

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
![【补题日记】[2022牛客暑期多校1]C-Grab the Seat](/img/86/1cf3bbc53d9365bb95dae6d532e276.png)
【补题日记】[2022牛客暑期多校1]C-Grab the Seat

Digital transformation behind the reshaping growth of catering chain stores

The new red envelope cover platform can build the source code of the independent background of the sub station

1000 okaleido tiger launched binance NFT, triggering a rush to buy

College degree want to 0 basic programming after looking for a job feasible?
随机推荐
Ggplot2 displays png
程序员必备技能----断点调试(IDEA版)
Summary of the first change to open source middleware keycloak
Seatunnel architecture
Today's code farmer girl learned about the express framework under node
STM32概念和安装【第一天】
Visual full link log tracking
Give me five minutes, give you a "cloud"
利用宝塔面板计划任务执行自动推送网址到百度收录
The difference between.Split (",", -1) and.Split (",")
Rylstim Screen Recorder
新红包封面平台可搭建分站独立后台的源码
On Domain Driven Design
Use the hiflow scene connector to view the epidemic situation in the region every day
Tensorflow 2.0 deep learning tutorial
Wechat applet setting background image does not display problem solution
Running around, market and quantitative page function optimization! Stock quantitative analysis tool qtyx-v2.4.5
LoadRunner12安装、录制第一个脚本以及代理服务器没有响应解决
NetApp FAS系列一个CIFS bug引起的控制器重启案例分享
1000 okaleido tiger launched binance NFT, triggering a rush to buy
