1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| class Solution { int ans = 0; public int rangeSumBST(TreeNode root, int low, int high) { dfs(root, low, high); return ans; }
public void dfs(TreeNode root, int low, int high){ if(root == null){ return; } dfs(root.left, low, high); if(root.val >=low && root.val <= high){ ans += root.val; } dfs(root.right, low, high); } }
|