在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★ Given the 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: 输入: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 提示:
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 }
|
请发表评论