Advertisement
Guest User

Untitled

a guest
Feb 22nd, 2019
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.66 KB | None | 0 0
  1. /**
  2. * Definition for a binary tree node.
  3. * public class TreeNode {
  4. * int val;
  5. * TreeNode left;
  6. * TreeNode right;
  7. * TreeNode(int x) { val = x; }
  8. * }
  9. */
  10. class Solution {
  11. public int rangeSumBST(TreeNode root, int L, int R) {
  12. int sum = 0;
  13. if (root != null) {
  14. if (R < root.val) {
  15. sum = rangeSumBST(root.left, L, R);
  16. } else if (L > root.val) {
  17. sum = rangeSumBST(root.right, L, R);
  18. } else {
  19. sum = rangeSumBST(root.left, L, root.val) + root.val +
  20. rangeSumBST(root.right, root.val, R);
  21. }
  22. }
  23. return sum;
  24. }
  25. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement