
Asked in Oracle
Remove BST Keys Outside Given Range
Given a Binary Search Tree (BST) and a specified range [min, max], your task is to remove all keys from the BST that fall outside this range. The BST should remain valid after modifications.
Input:
The first line contains an integer 'T', representing the number of test cases. Each test case involves: 1. An initial line describing the BST elements in level order format (use -1 for missing children). 2. A subsequent line containing two integers 'min' and 'max'.
Output:
For each test case, output a single line of 'N' space-separated integers, representing the inorder traversal of the adjusted BST.
Example:
If the BST elements are [4, 2, 7, 1, 3, -1, -1] and the range is [1, 3], the output should be [1, 2, 3] after removing keys outside the specified range.
Constraints:
- 1 <= T <= 5
- 1 <= N <= 10^5
- -10^3 <= nodeVal <= 10^3
Note:
There is no need to include print statements; these have already been incorporated elsewhere. Focus solely on implementing the necessary function.
Be the first one to answer
Add answer anonymously...
Stay ahead in your career. Get AmbitionBox app


Trusted by over 1.5 Crore job seekers to find their right fit company
80 L+
Reviews
10L+
Interviews
4 Cr+
Salaries
1.5 Cr+
Users
Contribute to help millions
AmbitionBox Awards
Get AmbitionBox app

