[Swift]LeetCode938. 二叉搜索樹的範圍和 | Range Sum of BST

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公衆號:山青詠芝(shanqingyongzhi)
➤博客園地址:山青詠芝(https://www.cnblogs.com/strengthen/
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:http://www.javashuo.com/article/p-omcazqpn-me.html 
➤若是連接不是山青詠芝的博客園地址,則多是爬取做者的文章。
➤原文已修改更新!強烈建議點擊原文地址閱讀!支持做者!支持原創!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★html

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

The binary search tree is guaranteed to have unique values.git

Example 1:github

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:less

  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(含)之間的全部結點的值的和。spa

二叉搜索樹保證具備惟一的值。 code

示例 1:htm

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

示例 2:blog

輸入: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 }
相關文章
相關標籤/搜索