LeetCode 450 - Delete Node in a BST
Lintcode: remove node from binary search tree | codesolutiony
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.
https://leetcode.com/problems/delete-node-in-a-bst/discuss/93296/Recursive-Easy-to-Understand-Java-Solution
https://leetcode.com/problems/delete-node-in-a-bst/discuss/93328/Java-Easy-to-Understand-Solution
http://hehejun.blogspot.com/2015/01/lintcoderemove-node-in-binary-search.html
删除的时候三种情况:
1) find the node with the given value;
2) if left subtree is NULL, return the right subtree
3) if right subtree is NULL, return the left subtree
4) find and delete the maximum node in the left subtree, and replace value( If we want to actually delete the node, that will be more complicated.)
In some case, maybe it's good to have the dummy root node.
https://leetcode.com/problems/delete-node-in-a-bst/discuss/93378/An-easy-understanding-O(h)-time-O(1)-space-Java-solution.
Read full article from Lintcode: remove node from binary search tree | codesolutiony
Lintcode: remove node from binary search tree | codesolutiony
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.
Given binary search tree:
5
/ \
3 6
/ \
2 4
Remove 3, you can either return:
5
/ \
2 6
\
4
or :
5
/ \
4 6
/
2
https://leetcode.com/problems/delete-node-in-a-bst/discuss/93296/Recursive-Easy-to-Understand-Java-Solution
https://leetcode.com/problems/delete-node-in-a-bst/discuss/93328/Java-Easy-to-Understand-Solution
- Recursively find the node that has the same value as the key, while setting the left/right nodes equal to the returned subtree
- Once the node is found, have to handle the below 4 cases
- node doesn't have left or right - return null
- node only has left subtree- return the left subtree
- node only has right subtree- return the right subtree
- node has both left and right - find the minimum value in the right subtree, set that value to the currently found node, then recursively delete the minimum value in the right subtree
public TreeNode deleteNode(TreeNode root, int key) {
if(root == null){
return null;
}
if(key < root.val){
root.left = deleteNode(root.left, key);
}else if(key > root.val){
root.right = deleteNode(root.right, key);
}else{
if(root.left == null){
return root.right;
}else if(root.right == null){
return root.left;
}
TreeNode minNode = findMin(root.right);
root.val = minNode.val;
root.right = deleteNode(root.right, root.val);
}
return root;
}
private TreeNode findMin(TreeNode node){
while(node.left != null){
node = node.left;
}
return node;
}
http://hehejun.blogspot.com/2015/01/lintcoderemove-node-in-binary-search.html
删除的时候三种情况:
1) find the node with the given value;
2) if left subtree is NULL, return the right subtree
3) if right subtree is NULL, return the left subtree
4) find and delete the maximum node in the left subtree, and replace value( If we want to actually delete the node, that will be more complicated.)
http://techinpad.blogspot.com/2015/05/lintcode-remove-node-in-binary-search.htmlpublic TreeNode removeNode(TreeNode root, int value) {if (root == null)return null;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 == null)return root.right;if (root.right == null)return root.left;TreeNode x = root;root = findMin(root.right);root.right = deleteMin(x.right);root.left = x.left;}return root;}
- TreeNode *getAndDeleteMax(TreeNode*parent, TreeNode* node)
- {
- while(node->right)
- {
- parent = node;
- node = node->right;
- }
- if(parent->left == node) parent->left = node->left;
- else parent->right = node->left;
- return node;
- }
- TreeNode* removeNode(TreeNode* root, int value)
- {
- if(!root) return NULL;
- if(root->val > value)
- {
- root->left = removeNode(root->left, value);
- }
- else if(root->val < value)
- {
- root->right = removeNode(root->right, value);
- }
- else
- {
- if(!root->left)
- {
- TreeNode * t = root->right;
- root->right = NULL; // not needed
- return t;
- }
- if(!root->right)
- {
- TreeNode *t = root->left;
- root->left = NULL; // this is not needed, as already have root->left = removeNode(root->left, value);
- return t;
- }
- TreeNode *leftMax = getAndDeleteMax(root, root->left);
- root->val = leftMax->val;
- }
- return root;
- }
In some case, maybe it's good to have the dummy root node.
public TreeNode removeNode(TreeNode root, int value) { TreeNode dummy = new TreeNode(0); dummy.left = root; TreeNode parent = findNode(dummy, root, value); TreeNode node; if (parent.left != null && parent.left.val == value) { node = parent.left; } else if (parent.right != null && parent.right.val == value) { node = parent.right; } else { return dummy.left; } deleteNode(parent, node); return dummy.left; } private TreeNode findNode(TreeNode parent, TreeNode node, int value) { if (node == null) { return parent; } if (node.val == value) { return parent; } if (value < node.val) { return findNode(node, node.left, value); } else { return findNode(node, node.right, value); } } private void deleteNode(TreeNode parent, TreeNode node) { if (node.right == null) { if (parent.left == node) { parent.left = node.left; } else { parent.right = node.left; } } else { TreeNode temp = node.right; TreeNode father = node; while (temp.left != null) { father = temp; temp = temp.left; } if (father.left == temp) { father.left = temp.right; } else { father.right = temp.right; } if (parent.left == node) { parent.left = temp; } else { parent.right = temp; } temp.left = node.left; temp.right = node.right; } }X. Iterative Version:
https://leetcode.com/problems/delete-node-in-a-bst/discuss/93378/An-easy-understanding-O(h)-time-O(1)-space-Java-solution.
If the node is found, delete the node.
We need a function
We need a function
deleteRoot
to delete the root from a BST.- If
root==null
, then returnnull
- If
root.right==null
, then returnroot.left
- If
root.right!=null
, the the new root of the BST is root.right; And what we should do is to put root.left into this new BST. As all nodes in root.left is smaller than the new tree, we just need to find the left-most node.
public TreeNode deleteNode(TreeNode root, int key) {
if (root==null || root.val==key) return deleteRoot(root);
TreeNode p=root;
while (true) { // search the node
if (key>p.val) {
if (p.right==null || p.right.val==key) {
p.right=deleteRoot(p.right);
break;
}
p=p.right;
}
else {
if (p.left==null || p.left.val==key) {
p.left=deleteRoot(p.left);
break;
}
p=p.left;
}
}
return root;
}
private TreeNode deleteRoot(TreeNode root) {
if (root==null) return null;
if (root.right==null) return root.left;
TreeNode x=root.right; // root.right should be the new root
while (x.left!=null) x=x.left; // find the left-most node
x.left=root.left;
return root.right;
}
https://leetcode.com/problems/delete-node-in-a-bst/discuss/93298/Iterative-solution-in-Java-O(h)-time-and-O(1)-space private TreeNode deleteRootNode(TreeNode root) {
if (root == null) {
return null;
}
if (root.left == null) {
return root.right;
}
if (root.right == null) {
return root.left;
}
TreeNode next = root.right;
TreeNode pre = null;
for(; next.left != null; pre = next, next = next.left);
next.left = root.left;
if(root.right != next) {
pre.left = next.right;
next.right = root.right;
}
return next;
}
public TreeNode deleteNode(TreeNode root, int key) {
TreeNode cur = root;
TreeNode pre = null;
while(cur != null && cur.val != key) {
pre = cur;
if (key < cur.val) {
cur = cur.left;
} else if (key > cur.val) {
cur = cur.right;
}
}
if (pre == null) {
return deleteRootNode(cur);
}
if (pre.left == cur) {
pre.left = deleteRootNode(cur);
} else {
pre.right = deleteRootNode(cur);
}
return root;
}
Find the node to be removed and its parent using binary search, and then use deleteRootNode to delete the root node of the subtree and return the new root node. This idea is taken from https://discuss.leetcode.com/topic/67309/an-easy-understanding-o-h-time-o-1-space-java-solution.
I'd also like to share my thinkings of the other solutions I've seen.
- There are many solutions that got high votes using recursive approach, including the ones from the Princeton's Algorithm and Data Structure book. Don't you notice that recursive approach always takes extra space? Why not consider the iterative approach first?
- Some solutions swap the values instead of swapping the nodes. In reality, the value of a node could be more complicated than just a single integer, so copying the contents might take much more time than just copying the reference.
- As for the case when both children of the node to be deleted are not null, I transplant the successor to replace the node to be deleted, which is a bit harder to implement than just transplant the left subtree of the node to the left child of its successor. The former way is used in many text books too. Why? My guess is that transplanting the successor can keep the height of the tree almost unchanged, while transplanting the whole left subtree could increase the height and thus making the tree more unbalanced
public TreeNode removeNode(TreeNode root, int value) {
TreeNode dummy = new TreeNode(-1), cur = root, pre = dummy;
dummy.right = root;
while (cur != null) {
if (cur.val == value) {
if (pre.right == cur) {
pre.right = makeNew(cur);
} else {
pre.left = makeNew(cur);
}
break;
} else if (cur.val < value) {
pre = cur;
cur = cur.right;
} else {
pre = cur;
cur = cur.left;
}
}
return dummy.right;
}
private TreeNode makeNew(TreeNode node) { // better method name
if (node.left == null && node.right == null) {
return null;
}
if (node.left == null || node.right == null) {
return node.left == null ? node.right : node.left;
}
TreeNode left = node.left.right;
TreeNode leftMost = node.right; //the left most node of right child
while (leftMost.left != null) {
leftMost = leftMost.left;
}
leftMost.left = left;//appen left's right tree to right's left most node
node.left.right = node.right;
return node.left;
}
Read full article from Lintcode: remove node from binary search tree | codesolutiony