894. Print BST Keys in Given Range

EasyTreesBSTDFSInorder

Given the root of a binary search tree and an inclusive range [lo, hi], return all keys within that range in ascending (sorted) order as an array. Use the BST property to prune. 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: Array — the in-range keys, sorted ascending.

Examples

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

Constraints

Asked by

TCSInfosysWiproCognizantCapgemini
Solve this problem in the editor →