Asked inOracle,Server Technology Engineer

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...
Oracle Server Technology Engineer Interview Questions
Stay ahead in your career. Get AmbitionBox app
qr-code
Helping over 1 Crore job seekers every month in choosing their right fit company
65 L+

Reviews

4 L+

Interviews

4 Cr+

Salaries

1 Cr+

Users/Month

Contribute to help millions

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

Follow us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter