bugfree Icon
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course

Leetcode Problem 669. Trim a Binary Search Tree

669. Trim a Binary Search Tree

Leetcode Solutions

Trimming a Binary Search Tree

  1. Define a recursive function trimBST(node, low, high) that takes the current node and the range [low, high] as arguments.
  2. If node is null, return null.
  3. If node.val is less than low, return the result of trimBST(node.right, low, high).
  4. If node.val is greater than high, return the result of trimBST(node.left, low, high).
  5. Recursively trim the left and right subtrees of node with trimBST(node.left, low, high) and trimBST(node.right, low, high), respectively.
  6. Update node.left and node.right with the trimmed subtrees.
  7. Return node as it is within the range and now has the correct trimmed subtrees.
UML Thumbnail

Ask Question

Programming Language
image/screenshot of info(optional)
Full Screen
Loading...

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...
bugfree Icon
OR