You are given the root of a Binary Search Tree (BST) and an integer key. Your task is to delete the node with the given key from the BST while maintaining the BST properties and return the root of the updated tree.
When deleting a node:
If the key does not exist, return the original tree unchanged.
Input: root = [5,3,6,2,4,null,7], key = 3
Output: [5,4,6,2,null,null,7]
Explanation: The node with value 3 is deleted while maintaining the BST property.
Input: root = [5,3,6,2,4,null,7], key = 0
Output: [5,3,6,2,4,null,7]
Explanation: The key does not exist, so the BST remains unchanged.
Input: root = [1], key = 1
Output: []
Explanation: The only node is deleted, resulting in an empty BST.
Approach, complexity and clean code in every language. Sign in to unlock the editorial.
Your submissions, results and runtime history live here. Sign in to start your streak.
50 30 70 20 40 60 80 20
50 30 70 40 60 80
40 20 60 10 30 50 70 30
40 20 60 10 50 70