Leetcode 938 Range Sum of BST

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/polanwind/article/details/88086311
/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
class Solution {
    public int rangeSumBST(TreeNode root, int L, int R) {
        int temp=0;
        if(root==null){
            return temp;
        }
        if(root.val>=L&&root.val<=R){
            temp+=root.val;
            temp+=(rangeSumBST(root.left, L, R));
            temp+=(rangeSumBST(root.right, L, R));
            return temp;    
        }
        else{
            temp+=(rangeSumBST(root.left, L, R));
            temp+=(rangeSumBST(root.right, L, R));
            return temp;
        }
    }
}

猜你喜欢

转载自blog.csdn.net/polanwind/article/details/88086311