Here is the interview question prompt, presented for reference.
A binary search tree
is a data structure that has the following properties,
- Each node in the tree has only two children.
- The left subtree consists of nodes with values less than the root node.
- The right subtree consists of nodes with values greater than the root node.
Consider that you are given a binary search tree as root
, and two integers low
and high
. Can you find the sum of values of all nodes that are in the inclusive range [low, high]
?
This problem can be best understood visually. Consider the example below. Nodes with values between the [7, 15] range are colored orange, and the sum of values of these nodes gives us the answer.
1 <= Node.val <= 105
1 <= low <= high <= 105
Node.val
are unique.You can see the full challenge with visuals at this link.
Challenges • Asked about 2 years ago by Jake from AlgoDaily
This is the main discussion thread generated for Sum Nodes Within Range for a Binary Search Tree (Main Thread).