450. Delete Node in a BST
题目描述
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.
Basically, the deletion can be divided into two stages:
Search for a node to remove.
If the node is found, delete the node.
Note: Time complexity should be O(height of tree).
|
|
题目大意
删除二叉树节点
解题思路
遍历二叉树,找到目标节点。
如果目标节点左子树或右子树为nil,则将目标节点与其替换。
如果目标节点左右子树都不为nil,则将左子树与目标节点替换,为使二叉树在删除节点后,仍然保留二叉树特性。需以左子树为key,重复以上操作。
代码
|
|