当前位置:网站首页>Leetcode 450. Delete node in binary search tree [binary search tree]
Leetcode 450. Delete node in binary search tree [binary search tree]
2022-06-13 02:22:00 【JackDawn!?】
subject

Ideas
If the value of the node to be deleted is greater than the current root node , Then the right subtree is recursively deleted ; If the value of the node to be deleted is less than the current root node , Recursively delete the left subtree ; If the current node is the node to be deleted , You need to consider which value replaces the value of the current root node :
- If the right subtree is not empty , Assign the smallest value in the right subtree to the current root node ;
- If the right subtree is empty , But the left subtree is not empty , Assign the maximum value in the left subtree to the current node ;
- If the left and right subtrees are empty , Then set the current node to null
Code
class Solution {
public:
TreeNode* deleteNode(TreeNode* root, int key) {
if(root == nullptr) return root;
else if(key > root->val) {
root->right = deleteNode(root->right, key);
} else if(key < root->val) {
root->left = deleteNode(root->left, key);
} else {
if(root->right) {
if(root->right->left) {
auto t = root->right;
auto pre = t;
while(t->left) {
pre = t;
t = t->left;
}
root->val = t->val;
pre->left = t->right;
delete t;
} else {
root->val = root->right->val;
auto t = root->right;
root->right = root->right->right;
delete t;
}
} else if(root->left) {
if(root->left->right) {
auto t = root->left;
auto pre = t;
while(t->right) {
pre = t;
t = t->right;
}
root->val = t->val;
pre->right = t->left;
delete t;
} else {
root->val = root->left->val;
auto t = root->left;
root->left = root->left->left;
delete t;
}
}
else {
return nullptr;
}
}
return root;
}
};
Execution results

边栏推荐
- Think about the possibility of attacking secure memory through mmu/tlb/cache
- Easydl related documents and codes
- [keras] generator for 3D u-net source code analysis py
- synchronized下的 i+=2 和 i++ i++执行结果居然不一样
- 传感器:MQ-5燃气模块测量燃气值(底部附代码)
- [learning notes] xr872 GUI littlevgl 8.0 migration (display part)
- Understand HMM
- Leetcode daily question - 890 Find and replace mode
- Paper reading - jukebox: a generic model for music
- Understand speech denoising
猜你喜欢

【Unity】打包WebGL项目遇到的问题及解决记录
![Leetcode 450. 删除二叉搜索树中的节点 [二叉搜索树]](/img/39/d5c4d424a160635791c4645d6f2e10.png)
Leetcode 450. 删除二叉搜索树中的节点 [二叉搜索树]

Share three stories about CMDB
![[pytorch]fixmatch code explanation (super detailed)](/img/22/66703bea0f8ee40eceb0687fcb3ad2.jpg)
[pytorch]fixmatch code explanation (super detailed)

I didn't expect that the index occupies several times as much space as the data MySQL queries the space occupied by each table in the database, and the space occupied by data and indexes. It is used i

Automatic differential reference

STM32F103 IIC OLED program migration complete engineering code

Barrykay electronics rushes to the scientific innovation board: it is planned to raise 360million yuan. Mr. and Mrs. Wang Binhua are the major shareholders

Common web page status return code crawler
![[reading paper] generate confrontation network Gan](/img/88/950b47cac330f208c0c9e100f42b4f.jpg)
[reading paper] generate confrontation network Gan
随机推荐
Bluetooth module: use problem collection
【 unity】 Problems Encountered in Packaging webgl Project and their resolution Records
Application circuit and understanding of BAT54C as power supply protection
Huawei equipment is configured with CE dual attribution
C language conditional compilation routine
[learning notes] xr872 GUI littlevgl 8.0 migration (display part)
json,xml,txt
Chapter7-13_ Dialogue State Tracking (as Question Answering)
Basic exercises of test questions Fibonacci series
STM32 sensorless brushless motor drive
Record: how to solve the problem of "the system cannot find the specified path" in the picture message uploaded by transferto() of multipartfile class [valid through personal test]
[single chip microcomputer] single timer in front and back platform program framework to realize multi delay tasks
Number of special palindromes in basic exercise of test questions
Sensor: sht30 temperature and humidity sensor testing ambient temperature and humidity experiment (code attached at the bottom)
Barrykay electronics rushes to the scientific innovation board: it is planned to raise 360million yuan. Mr. and Mrs. Wang Binhua are the major shareholders
ROS learning-6 detailed explanation of publisher programming syntax
Share three stories about CMDB
1000 fans ~
4.11 introduction to firmware image package
Queuing theory, game theory, analytic hierarchy process