Mark As Completed Discussion

Good evening! Here's our prompt for today.

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.

Question

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.

Try to solve this here or in Interactive Mode.

How do I practice this challenge?

JAVASCRIPT
OUTPUT
:001 > Cmd/Ctrl-Enter to run, Cmd/Ctrl-/ to comment

Here's our guided, illustrated walk-through.

How do I use this guide?