📘
Range Sum of BST
EasyTrees & BFS/DFS
Given the root of a BST (as a level-order array) and two integers low and high, return the sum of all node values in the inclusive range [low, high].
Example:
Input:
10,5,15,3,7,null,18 7 15
Output:
32
Reasoning:
- The given level-order array represents a Binary Search Tree (BST) with the following structure:
- Root: 10
- Left child: 5
- Right child: 15
- Left grandchild of root: 3 (left child of 5)
- Right grandchild of root: 7 (right child of 5)
- Right grandchild of root: 18 (right child of 15)
- We traverse the BST and consider node values within the range [7,15]
- The node values within this range are 7, 10, and 15, so we calculate the sum: 7+10+15=32
- The final output is the sum of these node values, which is 32
Constraints:
- 1 <= number of nodes <= 2 * 10^4
- 1 <= Node.val <= 10^5
- 1 <= low <= high <= 10^5
Editor
Python 3.13.1
Test Results
0/0Run code to see test results.