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.

AnswerBot
1mo

Remove keys from a BST that fall outside a specified range while maintaining its structure.

  • Use a recursive approach to traverse the BST.

  • For each node, check if its value is within the range [min, max]...read more

Help your peers!
Select
Add answer anonymously...

Josh Technology Group Software Developer Intern interview questions & answers

A Software Developer Intern was asked 3mo agoQ. Given a linked list, how do you ensure that it is in decreasing order?
A Software Developer Intern was asked 3mo agoQ. Given a random node in a BST, how do you check if a given element is part of the...read more
A Software Developer Intern was asked Q. Merge Two Binary Trees Problem Statement You are given the roots of two binary t...read more

Popular interview questions of Software Developer Intern

A Software Developer Intern was asked 3mo agoQ1. Given a linked list, how do you ensure that it is in decreasing order?
A Software Developer Intern was asked 3mo agoQ2. Given a random node in a BST, how do you check if a given element is part of the...read more
A Software Developer Intern was asked Q3. Merge Two Binary Trees Problem Statement You are given the roots of two binary t...read more
Josh Technology Group Software Developer Intern 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