Dsa
Trees
Medium
Trim BST Within Range
You are given a binary search tree (BST) and two integer values low and high. Trim the BST so that all its elements lie in [low, high]. You may modify the tree in-place.
Input format:
The input starts with an integer n (1 <= n <= 1000), representing the number of nodes. Followed by n lines containing node value and its left and right children values. The tree is guaranteed to be a valid BST. The last line contains two integers low and high.
Output format:
Print the inorder traversal of the trimmed BST as a space-separated string.
Example:
Input:
5
3 1 4
1 -1 -1
4 -1 5
5 -1 -1
4 5
Output:
4 5
Key concepts
treesbsttrimming
Practise this out loud — free
Start a mock interview on THIS exact question — a voice AI interviewer opens with it, pushes back like a real onsite, then hands you an instant scorecard.
🎙 Practise this question now