当前位置:网站首页>Leetcode skimming: binary tree 27 (delete nodes in the binary search tree)
Leetcode skimming: binary tree 27 (delete nodes in the binary search tree)
2022-07-07 12:48:00 【Taotao can't learn English】
450. Delete nodes in binary search tree
Given the root node of a binary search tree root And a value key, Delete the key Corresponding node , And keep the property of binary search tree unchanged . Back to binary search tree ( May be updated ) Reference to the root node of .
Generally speaking , Deleting a node can be divided into two steps :
First find the node to be deleted ;
If you find it , Delete it .
explain : The time complexity of the algorithm is required to be O ( h ) O(h) O(h),h Is the height of the tree .
Example :
Ideas
The node deletion of the search tree is much more complicated than the node addition , There are many situations to consider , Be prepared .
This question is not difficult , It will be finished in five hours . I don't want to write an explanation . Careful simulation , Follow the picture more , Be sure to draw .
package com.programmercarl.tree;
import com.programmercarl.util.GenerateTreeNode;
/** * @ClassName DeleteNode * @Descriotion TODO * @Author nitaotao * @Date 2022/7/6 15:08 * @Version 1.0 * https://leetcode.cn/problems/delete-node-in-a-bst/ * 450. Delete nodes in binary search tree **/
public class DeleteNode {
public static void main(String[] args) {
System.out.println(new Traversal().inorderTraversal(
new DeleteNode().deleteNode(GenerateTreeNode.generateTreeNode("[2,0,33,null,1,25,40,null,null,11,31,34,45,10,18,29,32,null,36,43,46,4,null,12,24,26,30,null,null,35,39,42,44,null,48,3,9,null,14,22,null,null,27,null,null,null,null,38,null,41,null,null,null,47,49,null,null,5,null,13,15,21,23,null,28,37,null,null,null,null,null,null,null,null,8,null,null,null,17,19,null,null,null,null,null,null,null,7,null,16,null,null,20,6]"), 33)
));
}
public TreeNode deleteNode(TreeNode root, int key) {
if (root == null) {
// The node of the current value does not exist
return null;
}
if (root.val == key) {
// The parent node does not exist
if (root.left != null && root.right == null) {
return root.left;
}
if (root.left == null && root.right != null) {
return root.right;
}
// There is only one node
if (root.left == null && root.right == null) {
return null;
}
// The rest is that both left and right nodes exist
TreeNode left = root.left;
TreeNode right = root.right;
if (root.right.left == null) {
// If the left side of the right child node does not exist
// Then the left side of the direct right child node is the left child node
root.right.left = left;
// Return to header node The right child node
return root.right;
} else if (root.left.right == null) {
// If the right side of the left child node does not exist
// Then the right side of the direct left child node is the right child node
root.left.right = right;
// Return to header node The right child node
return root.left;
} else {
// If the left and right subtrees have their own left and right subtrees
// Let the rightmost sub node of the left sub tree replace root The value of the can
TreeNode leftRightNode = left.right;
while (leftRightNode.right != null) {
left = left.right;
leftRightNode = leftRightNode.right;
}
// here leftRightNode It is the child node in the bottom right corner
int value = leftRightNode.val;
// Delete the bottom right node
left.right = null;
root.val = value;
return root;
}
}
TreeNode rootNode = root;
parentNode = root;
findNode(rootNode, key, false);
return root;
}
TreeNode parentNode;
public TreeNode findNode(TreeNode root, int key, boolean isLeft) {
if (root == null) {
// The node of the current value does not exist
return null;
}
if (root.val > key) {
parentNode = root;
findNode(root.left, key, true);
} else if (root.val < key) {
parentNode = root;
findNode(root.right, key, false);
} else {
// Find value
// root.val == key
// root There may be old and young
// The parent node The left child node The right child node
// If the parent node exists
if (root.left != null && root.right == null) {
// The left node of the current node exists , The right node does not exist
// The current node is the left child node of the parent node , That is, the parent node is larger than the current node
if (isLeft) {
parentNode.left = root.left;
} else {
// If the current node is the right child node of the parent node , That is, the parent node is smaller than the current node
parentNode.right = root.left;
}
return null;
}
if (root.left == null && root.right != null) {
// The left node of the current node does not exist , The right node exists
// The current node is the left child node of the parent node , That is, the parent node is larger than the current node
if (isLeft) {
parentNode.left = root.right;
} else {
// If the current node is the right child node of the parent node , That is, the parent node is smaller than the current node
parentNode.right = root.right;
}
return null;
}
if (root.left == null && root.right == null) {
// If the current node is a leaf node
if (isLeft) {
parentNode.left = null;
} else {
parentNode.right = null;
}
return null;
}
// Both left and right parent nodes exist
// If the current node is the left node of the parent node
// That is, the current node is smaller than the parent node
if (isLeft) {
// If the left and right subtrees have their own left and right subtrees
// Find the element in the bottom left corner of the right subtree to replace yourself
if (root.left == null && root.right == null) {
parentNode.left = null;
} else if (root.left == null && root.right != null) {
parentNode.left = root.right;
} else {
// Left Right It's not empty.
if (root.left.right == null) {
parentNode.left = root.left;
root.left.right = root.right;
} else {
// The lower right corner of the left subtree is not empty
TreeNode left = root.left;
TreeNode leftRightNode = left.right;
while (leftRightNode.right != null) {
left = left.right;
leftRightNode = leftRightNode.right;
}
int value = leftRightNode.val;
if (leftRightNode.left != null) {
left.right = leftRightNode.left;
} else {
left.right = null;
}
root.val = value;
return root;
}
}
return root;
} else {
// If the left and right subtrees have their own left and right subtrees
// Find the element in the bottom right corner of the left subtree to replace yourself
if (root.left == null && root.right == null) {
parentNode.right = null;
} else if (root.right == null && root.left != null) {
parentNode.right = root.left;
} else {
// Left Right It's not empty.
if (root.right.left == null) {
parentNode.right = root.right;
root.right.left = root.left;
} else {
// The lower left corner of the right subtree is not empty
TreeNode right = root.right;
TreeNode rightRightNode = right.left;
while (rightRightNode.left != null) {
right = right.left;
rightRightNode = rightRightNode.left;
}
int value = rightRightNode.val;
if (rightRightNode.right != null) {
right.left = rightRightNode.right;
} else {
right.left = null;
}
root.val = value;
return root;
}
}
return root;
}
}
return null;
}
}
边栏推荐
- MPLS experiment
- Polymorphism, final, etc
- [Q&A]AttributeError: module ‘signal‘ has no attribute ‘SIGALRM‘
- Static routing assignment of network reachable and telent connections
- Static comprehensive experiment
- Day-18 hash table, generic
- 【统计学习方法】学习笔记——逻辑斯谛回归和最大熵模型
- 广州市召开安全生产工作会议
- 2022危险化学品生产单位安全生产管理人员考题及在线模拟考试
- 2022 examination questions and online simulation examination for safety production management personnel of hazardous chemical production units
猜你喜欢
IPv6 experiment
Charles: four ways to modify the input parameters or return results of the interface
【PyTorch实战】图像描述——让神经网络看图讲故事
[statistical learning method] learning notes - support vector machine (Part 2)
Preorder, inorder and postorder traversal of binary tree
图像像素读写操作
Airserver automatically receives multi screen projection or cross device projection
The left-hand side of an assignment expression may not be an optional property access.ts(2779)
leetcode刷题:二叉树24(二叉树的最近公共祖先)
基于NeRF的三维内容生成
随机推荐
2022A特种设备相关管理(锅炉压力容器压力管道)模拟考试题库模拟考试平台操作
Creation and assignment of graphic objects
Configure an encrypted web server
2022危险化学品生产单位安全生产管理人员考题及在线模拟考试
基于NeRF的三维内容生成
leetcode刷题:二叉树22(二叉搜索树的最小绝对差)
Day-19 IO stream
2022 polymerization process test question simulation test question bank and online simulation test
ICLR 2022 | pre training language model based on anti self attention mechanism
金融数据获取(三)当爬虫遇上要鼠标滚轮滚动才会刷新数据的网页(保姆级教程)
2022-07-07日报:GAN发明者Ian Goodfellow正式加入DeepMind
SQL lab 26~31 summary (subsequent continuous update) (including parameter pollution explanation)
Guangzhou held work safety conference
Static vxlan configuration
数据库安全的重要性
What if does not match your user account appears when submitting the code?
leetcode刷题:二叉树26(二叉搜索树中的插入操作)
Static routing assignment of network reachable and telent connections
SQL lab 1~10 summary (subsequent continuous update)
MPLS experiment