美文网首页
LeetCode - Range Sum of BST

LeetCode - Range Sum of BST

作者: Andy1944 | 来源:发表于2019-07-17 18:24 被阅读0次

Range Sum of BST - LeetCode

Solution

class Solution {
    func rangeSumBST(_ root: TreeNode?, _ L: Int, _ R: Int) -> Int {
        if let root = root {
            if L > root.val {
                return rangeSumBST(root.right, L, R)
            } else if R < root.val {
                return rangeSumBST(root.left, L, R)
            } else {
                var sum = 0
                sum += rangeSumBST(root.left, L, R)
                sum += root.val
                sum += rangeSumBST(root.right, L, R)
                return sum
            }
        } else {
            return 0
        }
    }
}

相关文章

网友评论

      本文标题:LeetCode - Range Sum of BST

      本文链接:https://www.haomeiwen.com/subject/ggmclctx.html