• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

[Swift]LeetCode938.二叉搜索树的范围和|RangeSumofBST

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公众号:山青咏芝(shanqingyongzhi)
➤博客园地址:山青咏芝(https://www.cnblogs.com/strengthen/
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:
➤如果链接不是山青咏芝的博客园地址,则可能是爬取作者的文章。
➤原文已修改更新!强烈建议点击原文地址阅读!支持作者!支持原创!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★

热烈欢迎,请直接点击!!!

进入博主App Store主页,下载使用各个作品!!!

注:博主将坚持每月上线一个新app!!!

Given the root node of a binary search tree, return the sum of values of all nodes with value between L and R (inclusive).

The binary search tree is guaranteed to have unique values.

Example 1:

Input: root = 15
Output: 32

Example 2:

Input: root = 10
Output: 23

Note:

  1. The number of nodes in the tree is at most 10000.
  2. The final answer is guaranteed to be less than 2^31.

给定二叉搜索树的根结点 root,返回 L 和 R(含)之间的所有结点的值的和。

二叉搜索树保证具有唯一的值。 

示例 1:

输入:root = [10,5,15,3,7,null,18], L = 7, R = 15
输出:32

示例 2:

输入:root = [10,5,15,3,7,13,18,1,null,6], L = 6, R = 10
输出:23

提示:

  1. 树中的结点数量最多为 10000 个。
  2. 最终的答案保证小于 2^31

616ms
 1 /**
 2  * Definition for a binary tree node.
 3  * public class TreeNode {
 4  *     public var val: Int
 5  *     public var left: TreeNode?
 6  *     public var right: TreeNode?
 7  *     public init(_ val: Int) {
 8  *         self.val = val
 9  *         self.left = nil
10  *         self.right = nil
11  *     }
12  * }
13  */
14 class Solution {
15     func rangeSumBST(_ root: TreeNode?, _ L: Int, _ R: Int) -> Int {
16         var sum = 0
17         _rangeSumBST(root, L, R, &sum)
18         return sum
19     }
20     
21     func _rangeSumBST(_ root: TreeNode?, _ low: Int, _ high: Int, _ sum: inout Int) {
22         guard let root = root else { return }
23         if root.val >= low && root.val <= high {
24             sum += root.val
25         }
26         if root.val > low {
27             _rangeSumBST(root.left, low, high, &sum)
28         }
29         if root.val < high {
30             _rangeSumBST(root.right, low, high, &sum)
31         }
32     }
33 }

628ms

 1 /**
 2  * Definition for a binary tree node.
 3  * public class TreeNode {
 4  *     public var val: Int
 5  *     public var left: TreeNode?
 6  *     public var right: TreeNode?
 7  *     public init(_ val: Int) {
 8  *         self.val = val
 9  *         self.left = nil
10  *         self.right = nil
11  *     }
12  * }
13  */
14 class Solution {
15     func rangeSumBST(_ rt: TreeNode?, _ L: Int, _ R: Int) -> Int {
16       var sum = 0  
17       if let root = rt {
18         if root.val >= L && root.val <= R { sum += root.val }
19         sum += rangeSumBST(root.left, L, R)
20         sum += rangeSumBST(root.right, L, R)
21       }
22       return sum
23     }
24 }

632ms

 1 /**
 2  * Definition for a binary tree node.
 3  * public class TreeNode {
 4  *     public var val: Int
 5  *     public var left: TreeNode?
 6  *     public var right: TreeNode?
 7  *     public init(_ val: Int) {
 8  *         self.val = val
 9  *         self.left = nil
10  *         self.right = nil
11  *     }
12  * }
13  */ 
14 class Solution {
15     func rangeSumBST(_ root: TreeNode?, _ L: Int, _ R: Int) -> Int {
16         
17         guard let r = root else {
18             return 0
19         }
20         
21         if r.val < L {
22             return rangeSumBST(r.right, L, R)
23         } else if r.val > R {
24             return rangeSumBST(r.left, L, R)
25         } else {
26             return r.val + rangeSumBST(r.left, L, R) + rangeSumBST(r.right, L, R)
27         }
28     }
29 }

652ms

 1 /**
 2  * Definition for a binary tree node.
 3  * public class TreeNode {
 4  *     public var val: Int
 5  *     public var left: TreeNode?
 6  *     public var right: TreeNode?
 7  *     public init(_ val: Int) {
 8  *         self.val = val
 9  *         self.left = nil
10  *         self.right = nil
11  *     }
12  * }
13  */
14 class Solution{
15         var sum = 0;
16         func rangeSumBST(_ root: TreeNode?, _ L: Int, _ R: Int) -> Int {
17             sum = 0;
18             traverse(root, L, R)
19             return sum;
20         }
21         func traverse(_ root:TreeNode?, _ L:Int,_ R:Int) -> Void {
22             if(root != nil)
23             {
24                 if(L <= root!.val && R >= root!.val)
25                 {
26                     sum += root!.val;
27                 }
28                 traverse(root!.left, L, R);
29                 traverse(root!.right, L, R);
30             }
31         }
32     }

656ms
 1 /**
 2  * Definition for a binary tree node.
 3  * public class TreeNode {
 4  *     public var val: Int
 5  *     public var left: TreeNode?
 6  *     public var right: TreeNode?
 7  *     public init(_ val: Int) {
 8  *         self.val = val
 9  *         self.left = nil
10  *         self.right = nil
11  *     }
12  * }
13  */
14 class Solution {
15     func rangeSumBST(_ root: TreeNode?, _ L: Int, _ R: Int) -> Int {
16         return dfs(root,L,R)
17     }
18     
19     func dfs(_ root: TreeNode?, _ L: Int, _ R: Int) -> Int
20     {
21         if root == nil {return 0}
22         return dfs(root!.left, L, R) + dfs(root!.right, L, R) + (L <= root!.val && root!.val <= R ? root!.val : 0)
23     }
24 }

672ms
 1 /**
 2  * Definition for a binary tree node.
 3  * public class TreeNode {
 4  *     public var val: Int
 5  *     public var left: TreeNode?
 6  *     public var right: TreeNode?
 7  *     public init(_ val: Int) {
 8  *         self.val = val
 9  *         self.left = nil
10  *         self.right = nil
11  *     }
12  * }
13  */
14 class Solution {
15     func rangeSumBST(_ root: TreeNode?, _ L: Int, _ R: Int) -> Int {
16         if (root == nil) {
17             return 0;
18         }
19         if (root!.val < L) {
20             return rangeSumBST(root!.right, L, R)
21         }
22         if (root!.val > R) {
23             return rangeSumBST(root!.left, L, R)
24         }
25         return root!.val + rangeSumBST(root!.left, L, R) + rangeSumBST(root!.right, L, R)
26     }
27 }

 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
28.怎样在Swift中实现单例?发布时间:2022-07-13
下一篇:
Swift5.4语言指南(二十一)嵌套类型发布时间:2022-07-13
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap