Stealth Interview
  • Features
  • Pricing
  • Blog
  • Login
  • Sign up

Leetcode #450: Delete Node in a BST

In this guide, we solve Leetcode #450 Delete Node in a BST in Python and focus on the core idea that makes the solution efficient.

You will see the intuition, the step-by-step method, and a clean Python implementation you can use in interviews.

Leetcode

Problem Statement

Given a root node reference of a BST and a key, delete the node with the given key in the BST. Return the root node reference (possibly updated) of the BST.

Quick Facts

  • Difficulty: Medium
  • Premium: No
  • Tags: Tree, Binary Search Tree, Binary Tree

Intuition

The input is a tree, so recursive decomposition is a natural fit.

We can compute the answer by combining results from left and right subtrees.

Approach

Use DFS and pass the required state through recursive calls.

Combine child results to compute the answer for each node.

Steps:

  • Pick traversal order.
  • Recurse with state.
  • Combine results from children.

Example

Input: root = [5,3,6,2,4,null,7], key = 3 Output: [5,4,6,2,null,null,7] Explanation: Given key to delete is 3. So we find the node with value 3 and delete it. One valid answer is [5,4,6,2,null,null,7], shown in the above BST. Please notice that another valid answer is [5,2,6,null,4,null,7] and it's also accepted.

Python Solution

# Definition for a binary tree node. # class TreeNode: # def __init__(self, val=0, left=None, right=None): # self.val = val # self.left = left # self.right = right class Solution: def deleteNode(self, root: Optional[TreeNode], key: int) -> Optional[TreeNode]: if root is None: return None if root.val > key: root.left = self.deleteNode(root.left, key) return root if root.val < key: root.right = self.deleteNode(root.right, key) return root if root.left is None: return root.right if root.right is None: return root.left node = root.right while node.left: node = node.left node.left = root.left root = root.right return root

Complexity

The time complexity is O(n). The space complexity is O(h).

Edge Cases and Pitfalls

Watch for boundary values, empty inputs, and duplicate values where applicable. If the problem involves ordering or constraints, confirm the invariant is preserved at every step.

Summary

This Python solution focuses on the essential structure of the problem and keeps the implementation interview-friendly while meeting the constraints.


Ace your next coding interview

We're here to help you ace your next coding interview.

Subscribe
Stealth Interview
© 2026 Stealth Interview®Stealth Interview is a registered trademark. All rights reserved.
Product
  • Blog
  • Pricing
Company
  • Terms of Service
  • Privacy Policy