当前位置:网站首页>Leetcode 450 deleting nodes in a binary search tree
Leetcode 450 deleting nodes in a binary search tree
2022-07-06 00:18:00 【Hello, I'm Boger】
source : Power button (LeetCode)
link :https://leetcode-cn.com/problems/delete-node-in-a-bst
The question :
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 .
Example 1:
Input :root = [5,3,6,2,4,null,7], key = 3
Output :[5,4,6,2,null,null,7]
explain : Given the node value to be deleted is 3, So first we found 3 This node , Then delete it .
The right answer is [5,4,6,2,null,null,7], As shown in the figure below .
The other correct answer is [5,2,6,null,4,null,7].
Example 2:
Input : root = [5,3,6,2,4,null,7], key = 0
Output : [5,3,6,2,4,null,7]
explain : Binary tree does not contain a value of 0 The node of
Example 3:
Input : root = [], key = 0
Output : []
Tips :
Range of nodes [0, 104].
-105 <= Node.val <= 105
Unique node value
root Is a legitimate binary search tree
-105 <= key <= 105
Advanced : The time complexity of the algorithm is required to be O(h),h Is the height of the tree .
Ideas :
In fact, the operation of deleting binary search tree nodes was written in the data structure experiment class before , What I did at that time was , First find the deleted node in the tree , Then, the leftmost node in the right subtree of the deleted node will replace the position of the deleted node . Based on this idea, I wrote the following code :
class Solution {
public TreeNode deleteNode(TreeNode root, int key) {
TreeNode parent = null, parent1 = null, deleteNode = null, cur = root;
int leftOrRight = 0;// This variable is used to record whether the deleted node is the left node or the right node of the parent node ,0 Is the root node ,1 Represents the left node ,2 Represents the right node
// First find the node to delete
while (cur != null && cur.val != key) {
parent = cur;
if (cur.val > key) {
cur = cur.left;
leftOrRight = 1;
} else if (cur.val < key) {
cur = cur.right;
leftOrRight = 2;
}
}
if (cur == null) return root;// There are no nodes to delete
deleteNode = cur;
if (deleteNode.right == null) {
// The deleted node has no right subtree
if (leftOrRight == 0) return deleteNode.left;
else if (leftOrRight == 1) parent.left = deleteNode.left;
else parent.right = deleteNode.left;
return root;
}
// Find the leftmost node in the right subtree of the node to be deleted
cur = deleteNode.right;
if (cur.left == null) {
// The right subtree of the node to be deleted has no left son
cur.left = deleteNode.left;
if (leftOrRight == 0) return cur;
else if (leftOrRight == 1) parent.left = cur;
else parent.right = cur;
return root;
}
while (cur.left != null) {
parent1 = cur;
cur = cur.left;
}
parent1.left = cur.right;
cur.left = deleteNode.left;
cur.right = deleteNode.right;
if (leftOrRight == 0) return cur;
else if (leftOrRight == 1) parent.left = cur;
else parent.right = cur;
return root;
}
}
The above code can really meet the requirements of the problem , But it's too tedious , Because you need to consider all kinds of situations .
After I read the reference article , The idea is to delete the head node of the left subtree of the node ( Left the child ) Put it on the left child of the leftmost node of the right subtree of the deleted node , The process can be seen in the figure below ( Picture transferred from code Capriccio ), Then modify the above code , The code is under the picture :
class Solution {
public TreeNode deleteNode(TreeNode root, int key) {
TreeNode parent = null, deleteNode = null, cur = root;
int leftOrRight = 0;// This variable is used to record whether the deleted node is the left node or the right node of the parent node ,0 Is the root node ,1 Represents the left node ,2 Represents the right node
// First find the node to delete
while (cur != null && cur.val != key) {
parent = cur;
if (cur.val > key) {
cur = cur.left;
leftOrRight = 1;
} else if (cur.val < key) {
cur = cur.right;
leftOrRight = 2;
}
}
if (cur == null) return root;// There are no nodes to delete
deleteNode = cur;
if (deleteNode.right == null) {
// The deleted node has no right subtree
if (leftOrRight == 0) return deleteNode.left;
else if (leftOrRight == 1) parent.left = deleteNode.left;
else parent.right = deleteNode.left;
return root;
}
// Find the leftmost node in the right subtree of the node to be deleted
cur = deleteNode.right;
while (cur.left != null) cur = cur.left;
cur.left = deleteNode.left;
if (leftOrRight == 0) return deleteNode.right;
else if (leftOrRight == 1) parent.left = deleteNode.right;
else parent.right = deleteNode.right;
return root;
}
}
It can be seen that the code is concise , Some variables are also reduced .
Then I saw another wonderful approach in the reference article ( Refer to the deletion method of ordinary binary tree in the article ).
The idea of this approach is , Constantly exchange the value of the deleted node with the value of the leftmost node in the right subtree of the deleted node , Until the leftmost node in the exchanged right subtree does not have a right subtree , In this case, the left subtree of the node will be replaced directly .
Look at the code in detail , Note that this process may exchange values more than once , And the recursive method is used , It's also very concise .
If you don't understand the process , You can draw a picture on paper , You can understand the subtlety of this idea .
class Solution {
public TreeNode deleteNode(TreeNode root, int key) {
if (root == null) return null;
if (root.val == key) {
if (root.right == null) return root.left;
TreeNode cur = root.right;
while (cur.left != null) cur = cur.left;
root.val = cur.val;
cur.val = key;
}
root.left = deleteNode(root.left, key);
root.right = deleteNode(root.right, key);
return root;
}
}
边栏推荐
- Codeforces gr19 D (think more about why the first-hand value range is 100, JLS yyds)
- 什么叫做信息安全?包含哪些内容?与网络安全有什么区别?
- Doppler effect (Doppler shift)
- 云呐|公司固定资产管理系统有哪些?
- USB Interface USB protocol
- [QT] QT uses qjson to generate JSON files and save them
- 2022.7.5-----leetcode. seven hundred and twenty-nine
- Configuring OSPF load sharing for Huawei devices
- 【QT】Qt使用QJson生成json文件并保存
- How to solve the problems caused by the import process of ecology9.0
猜你喜欢
亲测可用fiddler手机抓包配置代理后没有网络
"14th five year plan": emphasis on the promotion of electronic contracts, electronic signatures and other applications
What are Yunna's fixed asset management systems?
Tools to improve work efficiency: the idea of SQL batch generation tools
【DesignMode】装饰者模式(Decorator pattern)
MySql——CRUD
权限问题:source .bash_profile permission denied
Atcoder beginer contest 254 [VP record]
Key structure of ffmpeg -- AVCodecContext
[noi simulation] Anaid's tree (Mobius inversion, exponential generating function, Ehrlich sieve, virtual tree)
随机推荐
[Chongqing Guangdong education] reference materials for Zhengzhou Vocational College of finance, taxation and finance to play around the E-era
Global and Chinese markets of universal milling machines 2022-2028: Research Report on technology, participants, trends, market size and share
The global and Chinese markets of dial indicator calipers 2022-2028: Research Report on technology, participants, trends, market size and share
什么叫做信息安全?包含哪些内容?与网络安全有什么区别?
SQLServer连接数据库读取中文乱码问题解决
【GYM 102832H】【模板】Combination Lock(二分图博弈)
How much do you know about the bank deposit business that software test engineers must know?
DEJA_ Vu3d - cesium feature set 055 - summary description of map service addresses of domestic and foreign manufacturers
Global and Chinese markets for pressure and temperature sensors 2022-2028: Research Report on technology, participants, trends, market size and share
shardingsphere源码解析
[noi simulation] Anaid's tree (Mobius inversion, exponential generating function, Ehrlich sieve, virtual tree)
【NOI模拟赛】Anaid 的树(莫比乌斯反演,指数型生成函数,埃氏筛,虚树)
Global and Chinese market of water heater expansion tank 2022-2028: Research Report on technology, participants, trends, market size and share
剖面测量之提取剖面数据
[Luogu p3295] mengmengda (parallel search) (double)
mysql-全局锁和表锁
【DesignMode】适配器模式(adapter pattern)
FFMPEG关键结构体——AVFormatContext
Knowledge about the memory size occupied by the structure
[designmode] composite mode