Remove Node in Binary Search Tree
ID: 87; hard; 删除二叉查找树的节点
Solution 1 (Java)
Notes
First, we use the property of the binary search tree to find the value-matching node. If we find that node, then we have 4 cases:
The node is a leaf node. We can directly delete it.
The node has one right child, we delete it by returning its right child.
The node has one left child, we delete it by returning its left child.
The node has two children. We find its predecessor node, which is the node that has the maximum value that is less than the node's value. We replace the node with its predecessor and remove the old position of its predecessor. Here, we can also use successor, which is the node that has the minimum value that is larger then the nodes' value.
Last updated