FastPrepDelete Node in a BST
Problems/Delete Node in a BST

Delete Node in a BST

Tokopedia logoTokopedia● MediumFULLTIMEPHONE SCREEN

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: The report names and links a well-known problem. We delete using the inorder successor, the usual choice, so that comparing the saved tree always gives the same answer. Our version is about 98% the same as the reported question.

You are given the root of a binary search tree and an integer key. Delete the node whose value equals key, if it exists, and return the updated root.

The returned tree must remain a binary search tree. When the deleted node has two children, replace its value with the smallest value in its right subtree, then delete that successor node. Using this rule means there is only one correct output tree.

Function

deleteNode(root: TreeNode, key: int) → TreeNode

Examples

Example 1

root = [5,3,6,2,4,null,7]key = 3return = [5,4,6,2,null,null,7]
root
532467
Removed
→
return
54267
Moved or new

Node 3 has two children. Its inorder successor is 4, which takes its place.

Example 2

root = [5,3,6,2,4,null,7]key = 0return = [5,3,6,2,4,null,7]
root
532467
→
return
532467

The key is absent, so the tree is unchanged.

Example 3

root = []key = 0return = []
root
empty
return
empty

An empty tree remains empty.

Constraints

  • The tree contains between 0 and 10000 nodes.
  • -100000 <= Node.val <= 100000.
  • Every node value is unique and the input is a valid binary search tree.
  • -100000 <= key <= 100000.

More Tokopedia interview questions

  • Interleaving StringPHONE SCREEN · Seen Jul 2021
  • Rotate ImagePHONE SCREEN · Seen Jul 2021
  • Validate Binary Search TreeONSITE INTERVIEW · Seen Aug 2021
  • Kth Largest Element in an ArrayONSITE INTERVIEW · Seen Jul 2021
  • Online Stock SpanONSITE INTERVIEW · Seen Jul 2021
  • Vertical Order Traversal of a Binary TreeONSITE INTERVIEW · Seen Jul 2021

Similar questions at other companies

  • AVL Insertion and RotationsSquarepoint Capital · PHONE SCREEN
  • Delete Node in a BSTBloomberg LP · PHONE SCREEN · ONSITE INTERVIEW
  • Convert a Sorted Linked List to a Balanced BSTMeta · PHONE SCREEN
  • Binary Tree Target-Sum PathsByteDance · PHONE SCREEN
  • Top K Pins with Insert and QueryPinterest · PHONE SCREEN
  • Construct a Binary Tree from Preorder and InordereBay · PHONE SCREEN
  • Filter Data with Composable PredicatesCoreWeave · PHONE SCREEN
  • Trim a Binary Search Tree to a RangeGoogle · PHONE SCREEN
  • Count Uni-Valued SubtreesAmazon · PHONE SCREEN
  • Reconstruct a BST from PreorderLyft · PHONE SCREEN
See Tokopedia hiring insights
Saved
/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution {
    public TreeNode deleteNode(TreeNode root, int key) {
        // Write your code here.
    }
}
Checking account…