- Remove Node in Binary Search Tree
中文English
Given a root of Binary Search Tree with unique value for each node. Remove the node with given value. If there is no such a node with given value in the binary search tree, do nothing. You should keep the tree still a binary search tree after removal.
Example
Example 1
Input:
Tree = {5,3,6,2,4}
k = 3
Output: {5,2,6,#,4} or {5,4,6,2}
Explanation:
Given binary search tree:
5
/
3 6
/
2 4
Remove 3, you can either return:
5
/
2 6
4
or
5
/
4 6
/
2
Example 2
Input:
Tree = {5,3,6,2,4}
k = 4
Output: {5,3,6,2}
Explanation:
Given binary search tree:
5
/
3 6
/
2 4
Remove 4, you should return:
5
/
3 6
/
2
解法1:这个思路比较简洁。基于PreOrder和递归。
思路:
首先看root是否为空,若是返回root。
然后看root->val是不是已经是value,如果是,
- 若root无左子树,返回右子树。
- 若root无右子树返回左子树。
- 若root的左右子树都存在,找root左子树中的最大值节点,找到后将root的右子树嫁接到最大值节点的右节点即可。当然,也可以找root右子树中的最小值节点,找到后将root的左子树嫁接到最小值节点的左节点。
代码如下:
/**
* Definition of TreeNode:
* class TreeNode {
* public:
* int val;
* TreeNode *left, *right;
* TreeNode(int val) {
* this->val = val;
* this->left = this->right = NULL;
* }
* }
*/
class Solution {
public:
/*
* @param root: The root of the binary search tree.
* @param value: Remove the node with given value.
* @return: The root of the binary search tree after removal.
*/
TreeNode * removeNode(TreeNode * root, int value) {
if (!root) return NULL;
if (root->val == value) {
if (!root->left) return root->right;
if (!root->right) return root->left;
//find the maximum node in left sub-tree
TreeNode * node = root->left;
while(node->right) {
node = node->right;
}
node->right = root->right;
return root->left;
}
if (root->val > value) {
root->left = removeNode(root->left, value);
} else {
root->right = removeNode(root->right, value);
}
return root;
}
};
二刷:上面的解法就是说,如果当前节点就是要找到节点,那么找到当前节点的前驱节点,然后删掉当前节点,接上前驱节点。
class Solution {
public:
/*
* @param root: The root of the binary search tree.
* @param value: Remove the node with given value.
* @return: The root of the binary search tree after removal.
*/
TreeNode * removeNode(TreeNode * root, int value) {
if (!root) return nullptr;
if (value < root->val) {
root->left = removeNode(root->left, value);
} else if (value > root->val) {
root->right = removeNode(root->right, value);
} else {
if (!root->left || !root->right) {
TreeNode *temp = (root->left) ? root->left : root->right;
delete(root);
return temp;
} else {
//find the predecessor
TreeNode *predecessor = root->left;
while(predecessor->right) predecessor = predecessor->right;
root->val = predecessor->val;
root->left = removeNode(root->left, predecessor->val);
}
}
return root;
}
};
解法2:跟解法1差不多,但是是找到当前节点的后续节点,然后删除当前节点,接上后续节点。
class Solution {
public:
/*
* @param root: The root of the binary search tree.
* @param value: Remove the node with given value.
* @return: The root of the binary search tree after removal.
*/
TreeNode * removeNode(TreeNode * root, int value) {
if (!root) return nullptr;
if (value < root->val) {
root->left = removeNode(root->left, value);
} else if (value > root->val) {
root->right = removeNode(root->right, value);
} else {
if (!root->left || !root->right) {
TreeNode *temp = (root->left) ? root->left : root->right;
delete(root);
return temp;
} else {
//find the successor
TreeNode *successor = root->right;
while(successor->left) successor = successor->left;
root->val = successor->val;
root->right = removeNode(root->right, successor->val);
}
}
return root;
}
};