Advertisement

Trim a Binary Search Tree - LeetCode 669 Solution

Trim a Binary Search Tree - Complete Solution Guide

Trim a Binary Search Tree is LeetCode problem 669, a Medium level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.

Problem Statement

Given the root of a binary search tree and the lowest and highest boundaries as low and high , trim the tree so that all its elements lies in [low, high] . Trimming the tree should not change the relative structure of the elements that will remain in the tree (i.e., any node's descendant should remain a descendant). It can be proven that there is a unique answer . Return the root of the trimmed binary search tree . Note that the root may change depending on the given bounds. Example 1: Input: ro

Detailed Explanation

The problem asks us to trim a given Binary Search Tree (BST) such that all node values fall within a specified range [low, high]. The trimming process should preserve the relative structure of the remaining nodes. The function needs to return the root of the newly trimmed BST, which might be different from the original root if the original root's value was outside the [low, high] range.

Solution Approach

The solution uses a recursive approach to traverse the BST. For each node, it checks if the node's value is within the range [low, high]. If the value is outside the range, the function recursively calls itself on either the left or right subtree, depending on whether the value is greater than 'high' or less than 'low', respectively. If the value is within the range, the function recursively trims the left and right subtrees and then returns the current node.

Step-by-Step Algorithm

  1. Step 1: Base Case: If the current node (root) is null, return null.
  2. Step 2: If the current node's value is greater than 'high', the node and its right subtree are outside the range. Recursively call trimBST on the left subtree (root.left) with the same 'low' and 'high' values and return the result. This effectively removes the current node and replaces it with the trimmed left subtree.
  3. Step 3: If the current node's value is less than 'low', the node and its left subtree are outside the range. Recursively call trimBST on the right subtree (root.right) with the same 'low' and 'high' values and return the result. This effectively removes the current node and replaces it with the trimmed right subtree.
  4. Step 4: If the current node's value is within the range [low, high], recursively trim the left subtree by calling trimBST on root.left, and assign the result back to root.left. Similarly, recursively trim the right subtree by calling trimBST on root.right, and assign the result back to root.right.
  5. Step 5: Return the current node (root).

Key Insights

  • Insight 1: The BST property is crucial for efficient trimming. Nodes smaller than 'low' can be completely discarded along with their left subtrees, and nodes larger than 'high' can be discarded along with their right subtrees.
  • Insight 2: Recursion provides a natural way to traverse the tree and modify it in place, adjusting the left and right children of each node based on the 'low' and 'high' values.
  • Insight 3: The base case for the recursion is when the current node is null; in this case, we simply return null.

Complexity Analysis

Time Complexity: O(N)

Space Complexity: O(H)

Topics

This problem involves: Tree, Depth-First Search, Binary Search Tree, Binary Tree.

Companies

Asked at: josh technology.