← DiffPush

Delete Node in a Binary Search Tree

Standard Bar Binary Search Trees · Practice Problems O(h) · O(h)

Retiring a Sorting Station

A mail-sorting hub must decommission one station without breaking the smaller-left, larger-right flow. A retiring station with no lines simply vanishes; with one line, its single branch takes over the slot; with two lines, the hub promotes the smallest station from the retiring one's right wing into its place, then quietly repeats the retirement on that promoted station's old spot.

Input: A binary search tree as a level-order array (null marks a missing child) and an integer key — the node value to remove.

Output: Return the tree after deletion, serialized as a level-order array with trailing nulls trimmed.

Constraints

Examples

Example 1

Input: {"tree":[5,3,6,2,4,null,7],"key":3}
Output: [5,4,6,2,null,null,7]
3's slot is filled by 4, the smallest value in its right wing, which is then removed from below.

Example 2

Input: {"tree":[5,3,6],"key":3}
Output: [5,null,6]
A leaf vanishes outright, leaving an empty branch.

Solve this in your browser →

Also on LeetCode ↗