package Recursion;
public class RangeSumBST_938 {
public class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode(int x) {
val = x;
}
}
int sum=0;
public int rangeSumBST(TreeNode root, int L, int R) {
return recur(root, L, R);
}
private int recur(TreeNode root, int l, int r) {
if (root != null) {
recur(root.left, l, r);
if (root.val >= l && root.val <= r) {
sum+=root.val;
}
recur(root.right, l, r);
}
return sum;
}
}
938. Range Sum of BST(递归)
最新推荐文章于 2021-02-02 16:23:47 发布