You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
classSolution {
intrangeSumBST(TreeNode? root, int low, int high) {
if (root ==null) return0; // base case.if (root.val < low)
returnrangeSumBST(root.right, low, high); // left branch excluded.if (root.val > high)
returnrangeSumBST(root.left, low, high); // right branch excluded.return root.val +rangeSumBST(root.right, low, high) +rangeSumBST(root.left, low, high); // count in both children.
}
}
Solution - 2
classSolution {
intrangeSumBST(TreeNode? root, int low, int high) {
if (root ==null) return0; // base case.return (low <= root.val && root.val <= high ? root.val :0) +rangeSumBST(root.right, low, high) +rangeSumBST(root.left, low, high);
}
}
Solution - 3
classSolution {
intrangeSumBST(TreeNode? root, int low, int high) {
List<TreeNode?> stk = [];
stk.add(root);
int sum =0;
while (stk.isNotEmpty) {
TreeNode? n = stk.removeLast();
if (n ==null) {
continue;
}
if (n.val > low) {
stk.add(n.left);
} // left child is a possible candidate.if (n.val < high) {
stk.add(n.right);
} // right child is a possible candidate.if (low <= n.val && n.val <= high) {
sum += n.val;
}
}
return sum;
}
}