mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-08 16:54:50 +08:00
Update
This commit is contained in:
@ -15,8 +15,9 @@ https://leetcode-cn.com/problems/3sum/
|
||||
|
||||
时间复杂度:O(n^2)
|
||||
|
||||
## 哈希法代码
|
||||
|
||||
## C++代码
|
||||
|
||||
### 哈希法代码
|
||||
|
||||
```
|
||||
class Solution {
|
||||
@ -54,7 +55,7 @@ public:
|
||||
}
|
||||
};
|
||||
```
|
||||
## 双指针法代码
|
||||
### 双指针法代码
|
||||
```
|
||||
class Solution {
|
||||
public:
|
||||
|
41
problems/0450.删除二叉搜索树中的节点.md
Normal file
41
problems/0450.删除二叉搜索树中的节点.md
Normal file
@ -0,0 +1,41 @@
|
||||
## 题目地址
|
||||
https://leetcode-cn.com/problems/delete-node-in-a-bst/
|
||||
|
||||
## 思路
|
||||
|
||||
平衡二叉树中删除节点有三种情况
|
||||
|
||||
1. 找到删除的节点,其左节点为空,那么就返回其右节点
|
||||
2. 找到删除的节点,其右节点为空,那么就返回其左节点
|
||||
3. 找到删除的节点,左右孩子节点都不为空,则将左孩子放到右孩子的最左面的节点的左孩子上。
|
||||
|
||||
|
||||
## C++代码
|
||||
|
||||
```
|
||||
class Solution {
|
||||
public:
|
||||
TreeNode* deleteNode(TreeNode* root, int key) {
|
||||
if (root == NULL) return root;
|
||||
if (root->val == key) {
|
||||
if (root->left == NULL) return root->right; // 第一种情况
|
||||
else if (root->right == NULL) return root->left; // 第二种情况
|
||||
else { // 第三种情况
|
||||
TreeNode* cur = root->right;
|
||||
while(cur->left != NULL) {
|
||||
cur = cur->left;
|
||||
}
|
||||
cur->left = root->left;
|
||||
root = root->right;
|
||||
return root;
|
||||
}
|
||||
}
|
||||
if (root->val > key) root->left = deleteNode(root->left, key);
|
||||
if (root->val < key) root->right = deleteNode(root->right, key);
|
||||
return root;
|
||||
}
|
||||
};
|
||||
```
|
||||
|
||||
|
||||
> 更多算法干货文章持续更新,可以微信搜索「代码随想录」第一时间围观,关注后,回复「Java」「C++」 「python」「简历模板」「数据结构与算法」等等,就可以获得我多年整理的学习资料。
|
@ -40,5 +40,5 @@ public:
|
||||
};
|
||||
```
|
||||
|
||||
> 更过算法干货文章持续更新,可以微信搜索「代码随想录」第一时间围观,关注后,回复「Java」「C++」 「python」「简历模板」「数据结构与算法」等等,就可以获得我多年整理的学习资料。
|
||||
> 更多算法干货文章持续更新,可以微信搜索「代码随想录」第一时间围观,关注后,回复「Java」「C++」 「python」「简历模板」「数据结构与算法」等等,就可以获得我多年整理的学习资料。
|
||||
|
||||
|
Reference in New Issue
Block a user