Oracle Logo

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
Select
Add answer anonymously...
undefined Interview Questions
Stay ahead in your career. Get AmbitionBox app
play-icon
play-icon
qr-code
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

Made with ❤️ in India. Trademarks belong to their respective owners. All rights reserved © 2025 Info Edge (India) Ltd.

Follow Us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter
Profile Image
Hello, Guest
AmbitionBox Employee Choice Awards 2025
Winners announced!
awards-icon
Contribute to help millions!
Write a review
Write a review
Share interview
Share interview
Contribute salary
Contribute salary
Add office photos
Add office photos
Add office benefits
Add office benefits