Delete a leaf node
Input
root = build_bst([5,3,7,2,4,6,8]); key = 2 result = inorder(delete_node(root, 2))
Output
[3, 4, 5, 6, 7, 8]
2 is a leaf. After deletion, the inorder traversal no longer includes 2.
Full lesson preview
Remove a node with a given key from a binary search tree and return the new root.
Problem statement
Task
Examples
Input
root = build_bst([5,3,7,2,4,6,8]); key = 2 result = inorder(delete_node(root, 2))
Output
[3, 4, 5, 6, 7, 8]
2 is a leaf. After deletion, the inorder traversal no longer includes 2.
Input format
Output format
Constraints
Samples
Input
delete_node(build_bst([5,3,7,2,4,6,8]), 3)
Output
[2, 4, 5, 6, 7, 8]
Node 3 has two children; it is replaced by its inorder successor 4.