This commit is contained in:
youngyangyang04
2020-08-03 09:03:18 +08:00
parent 55ee775ff9
commit fc93bf9bc2
4 changed files with 47 additions and 4 deletions

View File

@ -15,8 +15,9 @@ https://leetcode-cn.com/problems/3sum/
时间复杂度O(n^2)
## 哈希法代码
## C++代码
### 哈希法代码
```
class Solution {
@ -54,7 +55,7 @@ public:
}
};
```
## 双指针法代码
### 双指针法代码
```
class Solution {
public:

View 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」「简历模板」「数据结构与算法」等等就可以获得我多年整理的学习资料。

View File

@ -40,5 +40,5 @@ public:
};
```
> 算法干货文章持续更新可以微信搜索「代码随想录」第一时间围观关注后回复「Java」「C++」 「python」「简历模板」「数据结构与算法」等等就可以获得我多年整理的学习资料。
> 算法干货文章持续更新可以微信搜索「代码随想录」第一时间围观关注后回复「Java」「C++」 「python」「简历模板」「数据结构与算法」等等就可以获得我多年整理的学习资料。