23 · Binary Search Trees

#450 Delete Node in a BST

Mediumsolve on LeetCode ↗

frequently reported atOracleUberByteDance / TikTokFlipkartAmazonBloomberg

the problem

In plain words

Delete the node with value key (if present) and return the root of the still-valid BST.

in root = [5,3,6,2,4,null,7], key = 3

out [5,4,6,2,null,null,7]

3 has two children; its successor 4 replaces it.

in root = [5,3,6,2,4,null,7], key = 0

out [5,3,6,2,4,null,7]

0 is not in the tree.

in root = [], key = 0

out []

  • 0 ≤ number of nodes ≤ 10⁴
  • -10⁵ ≤ Node.val, key ≤ 10⁵
  • Values are unique
  • Follow-up: O(height)

your turn

Try it before you peek

Read the examples, say the brute force out loud, then write code: here or on LeetCode ↗. Stuck for 20 minutes? Open one hint at a time.

Solve it in the browser

LeetCode-style: complete the class. Compiled by the real javac (Java 17) and judged right here — nothing is sent anywhere.

    Solutions are hidden so you can try first. Even 20 minutes of real effort makes the answer stick far better than reading it.

    Marking the problem solved, or opening every hint, also shows it.

    practise next

    Similar problems

    1. #701
    2. #285
      Inorder Successor in BST
      Mediumin BSTLeetCode Premium
    3. #98