16. Remove BST Keys Outside Given Range
β GFG solution to Remove BST Keys Outside Given Range: efficiently trim a BST to keep only nodes within a specified range using recursive DFS. π
The problem can be found at the following link: π Question Link
π§© Problem Description
Given the root of a Binary Search Tree (BST) and two integers l
and r
, remove all the nodes whose values lie outside the range [l, r]
.
Note: The modified tree should also be a BST and the sequence of the remaining nodes should not be changed.
π Examples
Example 1
Input: root = [6, -13, 14, N, -8, 13, 15, N, N, 7], l = -10, r = 13
Output: [6, -8, 13, N, N, 7]
Explanation: All the nodes outside the range [-10, 13] are removed and the modified tree is a valid BST.
Example 2
Input: root = [14, 4, 16, 2, 8, 15, N, -8, 3, 7, 10], l = 2, r = 6
Output: [4, 2, N, N, 3]
Explanation: All the nodes outside the range [2, 6] are removed and the modified tree is a valid BST.
π Constraints
$1 \le \text{number of nodes} \le 10^4$
$1 \le \text{node->data} \le 10^4$
$1 \le l \le r \le 10^4$
β
My Approach
The optimal approach uses Post-Order Recursive Traversal with BST Property Optimization:
Recursive DFS with BST Pruning
Base Case:
If the current node is
NULL
, returnNULL
immediately.
Post-Order Recursion:
Recursively process the left subtree first:
root->left = removekeys(root->left, l, r)
.Then recursively process the right subtree:
root->right = removekeys(root->right, l, r)
.
Node Validation:
If
root->data < l
: The current node is below the valid range. Since it's a BST, all nodes in the left subtree are also belowl
. Return the right subtree.If
root->data > r
: The current node is above the valid range. Since it's a BST, all nodes in the right subtree are also abover
. Return the left subtree.If
root->data
is within[l, r]
: Keep the current node and return it with updated children.
BST Property Utilization:
Leverage the BST property where left children are smaller and right children are larger.
This allows early pruning of entire subtrees without visiting every node.
Return Modified Root:
The function returns the new root of the modified subtree at each level.
π Time and Auxiliary Space Complexity
Expected Time Complexity: O(n), where n is the number of nodes in the BST. In the worst case, we visit each node once to determine if it should be kept or removed.
Expected Auxiliary Space Complexity: O(h), where h is the height of the BST. This space is used by the recursion call stack. In the worst case of a skewed tree, h = n, but for a balanced BST, h = log(n).
π§βπ» Code (C++)
class Solution {
public:
Node* removekeys(Node* root, int l, int r) {
if (!root) return NULL;
root->left = removekeys(root->left, l, r);
root->right = removekeys(root->right, l, r);
if (root->data < l) return root->right;
if (root->data > r) return root->left;
return root;
}
};
β Code (Java)
class Solution {
Node removekeys(Node root, int l, int r) {
if (root == null) return null;
root.left = removekeys(root.left, l, r);
root.right = removekeys(root.right, l, r);
if (root.data < l) return root.right;
if (root.data > r) return root.left;
return root;
}
}
π Code (Python)
class Solution:
def removekeys(self, root, l, r):
if not root: return None
root.left = self.removekeys(root.left, l, r)
root.right = self.removekeys(root.right, l, r)
if root.data < l: return root.right
if root.data > r: return root.left
return root
π§ Contribution and Support
For discussions, questions, or doubts related to this solution, feel free to connect on LinkedIn: π¬ Any Questions?. Let's make this learning journey more collaborative!
β If you find this helpful, please give this repository a star! β
πVisitor Count
Last updated