출처 : https://leetcode.com/problems/range-sum-of-bst/submissions/
Given the root
node of a binary search tree and two integers low
and high
, return the sum of values of all nodes with a value in the inclusive range [low, high]
.
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
public int rangeSumBST(TreeNode root, int low, int high) {
if (root == null) return 0;
int sum = 0;
if (low <= root.val && root.val <= high) {
sum += root.val;
}
sum += rangeSumBST(root.left, low, high);
sum += rangeSumBST(root.right, low, high);
return sum;
}
}
🙈 풀이 참조한 문제
할 때마다 모르겠어요 ..