Range Sum of BST
ID: 938; easy
Solution 1 (Java)
Notes
We utilize the condition that the tree is a BST. Note that you can use a normal traversal to traverse all the nodes, but it is not necessary to visit some nodes.
If
root.val
is already less than or equal tolow
(less thanhigh
of course), we only traverse its right subtree. Ifroot.val
is already larger than or equal tohigh
(more thanlow
), we only traverse its left subtree.
Last updated