题目
给定二叉搜索树的根结点 root,返回值位于范围 [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 {
int sum = 0;
public int rangeSumBST(TreeNode root, int low, int high) {
if (root == null) return sum;
if (root.val >= low && root.val <= high) sum += root.val;
rangeSumBST(root.left, low, high);
rangeSumBST(root.right, low, high);
return sum;
}
}