Recursive Algorithm to Cut a Binary Search Tree (Remove Nodes Not In Range)


Given a binary search tree root, an integer lo, and another an integer hi, remove all nodes that are not between [lo, hi] inclusive.

Constraints
n ≤ 100,000 where n is the number of nodes in root
cutting-a-binary-search-tree Recursive Algorithm to Cut a Binary Search Tree (Remove Nodes Not In Range) algorithms c / c++ python recursive

Remove Nodes from Binary Search Tree

If the current node is outside the given range, we can either cut off it and its left or right branch accordingly. Otherwise, we keep the current root node, and recursively remove nodes from its left and right branch. This algorithm then recursively cuts off un-wanted branches/nodes from a Binary Search Tree.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
# class Tree:
#     def __init__(self, val, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution:
    def cutBinarySearchTree(self, root, lo, hi):
        if root is None:
            return root
        if root.val < lo:
            return self.solve(root.right, lo, hi)
        if root.val > hi:
            return self.solve(root.left, lo, hi)
        root.left = self.solve(root.left, lo, hi)
        root.right = self.solve(root.right, lo, hi)
        return root
# class Tree:
#     def __init__(self, val, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution:
    def cutBinarySearchTree(self, root, lo, hi):
        if root is None:
            return root
        if root.val < lo:
            return self.solve(root.right, lo, hi)
        if root.val > hi:
            return self.solve(root.left, lo, hi)
        root.left = self.solve(root.left, lo, hi)
        root.right = self.solve(root.right, lo, hi)
        return root

The time complexity is O(N) and the space complexity is O(N) due to stack usage from Recursion.

C++ Implementation of Trimming a Binary Search Tree using Recursion:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
/**
 * class Tree {
 *     public:
 *         int val;
 *         Tree *left;
 *         Tree *right;
 * };
 */
Tree* cutBinarySearchTree(Tree* root, int lo, int hi) {
    if (!root) return root;
    if (root->val < lo) return cutBinarySearchTree(root->right, lo, hi);
    if (root->val > hi) return cutBinarySearchTree(root->left, lo, hi);
    root->left = cutBinarySearchTree(root->left, lo, hi);
    root->right = cutBinarySearchTree(root->right, lo, hi);
    return root;
}
/**
 * class Tree {
 *     public:
 *         int val;
 *         Tree *left;
 *         Tree *right;
 * };
 */
Tree* cutBinarySearchTree(Tree* root, int lo, int hi) {
    if (!root) return root;
    if (root->val < lo) return cutBinarySearchTree(root->right, lo, hi);
    if (root->val > hi) return cutBinarySearchTree(root->left, lo, hi);
    root->left = cutBinarySearchTree(root->left, lo, hi);
    root->right = cutBinarySearchTree(root->right, lo, hi);
    return root;
}

See Also: How to Delete Nodes from Binary Tree and Make a Forest?

And here is another post on Trimming Bianry Search Tree: Teaching Kids Programming – Recursive Algorithm to Cut/Trim a Binary Search Tree

–EOF (The Ultimate Computing & Technology Blog) —

GD Star Rating
loading...
451 words
Last Post: Teaching Kids Programming - Introduction to ASCII
Next Post: Teaching Kids Programming - Add One to List

The Permanent URL is: Recursive Algorithm to Cut a Binary Search Tree (Remove Nodes Not In Range)

Leave a Reply