为有牺牲多壮志,敢教日月换新天。

[Swift]LeetCode938. 二叉搜索树的范围和 | Range Sum of BST

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

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

进入博主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 = [10,5,15,3,7,null,18], L = 7, R = 15
Output: 32

Example 2:

Input: root = [10,5,15,3,7,13,18,1,null,6], L = 6, R = 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 }

 

posted @ 2018-11-11 13:32  为敢技术  阅读(2002)  评论(0编辑  收藏  举报