Delete a Key From a BST While Preserving the Ordering

Last updated: November 29, 2026

Quick Overview

Remove a given key from a binary search tree of user IDs and return the new root, keeping the tree a valid BST. If the key is absent, return the tree unchanged.

Google
Coding & Algorithms
Software Engineer
Google
November 29, 2026
Software Engineer
Technical Screen
Coding & Algorithms
Medium

0

0

0 solved


Remove a given key from a binary search tree of user IDs and return the new root, keeping the tree a valid BST. If the key is absent, return the tree unchanged.

A Google technical screen question about getting all three deletion cases right without losing subtrees. The interviewer often asks why the in-order successor is a safe replacement and what happens to tree balance over many deletions.

What the Interviewer Expects
  • Search for the key using BST order
  • Handle leaf, one-child and two-children cases
  • Replace with the in-order successor (or predecessor) in the two-child case
  • Return the possibly new root
  • Discuss height impact and balancing
Key Topics to Cover
binary search tree
deletion
in-order successor
recursion
How to Approach This
  1. Clarify input constraints and edge cases before writing code.
  2. Walk through your approach verbally and confirm with the interviewer before coding.
  3. Start with a brute force solution, then optimize. Mention time and space complexity.
  4. Test your solution with examples, including edge cases like empty input or duplicates.
  5. Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
  • Do it iteratively with parent tracking.
  • Why is the in-order successor guaranteed to have no left child?
  • How do self-balancing trees handle deletion?
Sharpen Your Skills on Codemia

Practice similar problems with our interactive workspace, get AI feedback, and track your progress.

Practice DSA Problems
Sample Answer
Requirements

Input: BST root and key. Output: root after deletion. Keys are unique. If the key is missing, nothing changes. The root itself may be deleted, so the function must return the new root....

High-Level Design

Recurse toward the key using BST order, reassigning node.left or node.right to the result of the recursive call so that changes below are linked in. When the key is found: with no left child, retu...


Submit Your Answer
Markdown supported