Community

Start a Thread


Notifications
Subscribe You’re not receiving notifications from this thread.

Sum Nodes Within Range for a Binary Search Tree (Main Thread)

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.

![image](https://storage.googleapis.com/algodailyrandomassets/curriculum/binary-search-trees/sum-nodes-within-range-bst/problem.png)

Constraints

  • The number of nodes in the tree is in the range [1, 2 * 104].
  • 1 <= Node.val <= 105
  • 1 <= low <= high <= 105
  • All 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

Jake from AlgoDaily Commented on Aug 27, 2022:

This is the main discussion thread generated for Sum Nodes Within Range for a Binary Search Tree (Main Thread).