888. Count Nodes in BST with Keys in Given Range

EasyTreesBSTDFS

Given the root of a binary search tree and an inclusive range [lo, hi], count the number of nodes whose keys fall within that range. Use the BST property to prune subtrees. The input gives a level-order BST array and 'lo hi' separated by ' | '.

Input: A level-order BST array and 'lo hi', separated by ' | '.

Output: Integer — the count of in-range keys.

Examples

Example 1
Input: [8,3,10,1,6,null,14] | 4 10
Output: 3
Explanation: Keys 6, 8, 10 fall within [4,10].
Example 2
Input: [5] | 1 10
Output: 1
Explanation: Single key in range.
Example 3
Input: [5] | 6 10
Output: 0
Explanation: No keys in range.

Constraints

Asked by

TCSInfosysWiproCognizantCapgemini
Solve this problem in the editor →