/**
* 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 (null == root) {
return 0;
}
Queue<TreeNode> queue =new LinkedList();
queue.add(root);
int result =0;
while(queue.size() >0) {
int size = queue.size();
while (size >0) {
TreeNode temp = queue.poll();
if (low <= temp.val && temp.val <=high) {
result =result +temp.val;
}
if (temp.left !=null) {
queue.add(temp.left);
}
if (temp.right !=null) {
queue.add(temp.right);
}
size--;
}
}
return result;
}
}