Skip to content

Commit ff4aaef

Browse files
Update
1 parent 748a9ac commit ff4aaef

File tree

6 files changed

+255
-2
lines changed

6 files changed

+255
-2
lines changed

README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
|[0059.螺旋矩阵II](https://github.com/youngyangyang04/leetcode/blob/master/problems/0059.螺旋矩阵II.md) |数组 |中等|**模拟**|
4040
|[0083.删除排序链表中的重复元素](https://github.com/youngyangyang04/leetcode/blob/master/problems/0083.删除排序链表中的重复元素.md) |链表 |简单|**模拟**|
4141
|[0094.二叉树的中序遍历](https://github.com/youngyangyang04/leetcode/blob/master/problems/0094.二叉树的中序遍历.md) ||中等|**递归** **迭代/栈**|
42+
|[0098.验证二叉搜索树](https://github.com/youngyangyang04/leetcode/blob/master/problems/0098.验证二叉搜索树.md) ||中等|**递归**|
4243
|[0100.相同的树](https://github.com/youngyangyang04/leetcode/blob/master/problems/0100.相同的树.md) ||简单|**递归** |
4344
|[0101.对称二叉树](https://github.com/youngyangyang04/leetcode/blob/master/problems/0101.对称二叉树.md) ||简单|**递归** **迭代/队列/栈**|
4445
|[0104.二叉树的最大深度](https://github.com/youngyangyang04/leetcode/blob/master/problems/0104.二叉树的最大深度.md) ||简单|**递归** **队列/BFS**|
@@ -67,7 +68,9 @@
6768
|[0383.赎金信](https://github.com/youngyangyang04/leetcode/blob/master/problems/0383.赎金信.md) |数组 |简单|**暴力** **字典计数** **哈希**|
6869
|[0434.字符串中的单词数](https://github.com/youngyangyang04/leetcode/blob/master/problems/0434.字符串中的单词数.md) |字符串 |简单|**模拟**|
6970
|[0454.四数相加II](https://github.com/youngyangyang04/leetcode/blob/master/problems/0454.四数相加II.md) |哈希表 |中等| **哈希**|
70-
|[0575.分糖果.md](https://github.com/youngyangyang04/leetcode/blob/master/problems/0575.分糖果.md) |哈希表 |简单|**哈希**|
71+
|[0575.分糖果](https://github.com/youngyangyang04/leetcode/blob/master/problems/0575.分糖果.md) |哈希表 |简单|**哈希**|
72+
|[0617.合并二叉树](https://github.com/youngyangyang04/leetcode/blob/master/problems/0617.合并二叉树.md) ||简单|**递归** **迭代**|
73+
|[0654.最大二叉树](https://github.com/youngyangyang04/leetcode/blob/master/problems/0654.最大二叉树.md) ||中等|**递归**|
7174
|[0705.设计哈希集合](https://github.com/youngyangyang04/leetcode/blob/master/problems/0705.设计哈希集合.md) |哈希表 |简单|**模拟**|
7275
|[0707.设计链表](https://github.com/youngyangyang04/leetcode/blob/master/problems/0707.设计链表.md) |链表 |中等|**模拟**|
7376
|[1047.删除字符串中的所有相邻重复项](https://github.com/youngyangyang04/leetcode/blob/master/problems/1047.删除字符串中的所有相邻重复项.md) ||简单|****|
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
## 题目地址
2+
3+
## 思路
4+
5+
这道题目比较容易陷入两个陷阱:
6+
* 陷阱1 :[10,5,15,null,null,6,20] 这个case 要考虑道
7+
* 陷阱2:样例中根节点的val 可能是-2147483648
8+
9+
中序遍历之后 输出的顺序就应该是一个从大到小的顺序, 可以使用一个全局变量进行比较。
10+
11+
## C++代码
12+
13+
14+
[10,5,15,null,null,6,20] 为什么预期结果是 false.... 这是经典陷阱
15+
错误代码
16+
```
17+
class Solution {
18+
public:
19+
bool isValidBST(TreeNode* root) {
20+
if (root == NULL) return true;
21+
22+
if (root->left != NULL && root->right != NULL) {
23+
if (root->val > root->left->val && root->val < root->right->val) {
24+
return true;
25+
} else {
26+
return false;
27+
}
28+
}
29+
30+
if (root->left != NULL && root->right == NULL) {
31+
if (root->val > root->left->val) {
32+
return true;
33+
}else {
34+
return false;
35+
}
36+
}
37+
if (root->left == NULL && root->right != NULL) {
38+
if (root->val < root->right->val) {
39+
return true;
40+
}else {
41+
return false;
42+
}
43+
}
44+
if (root->left == NULL && root->right == NULL) return true;
45+
return isValidBST(root->left) && isValidBST(root->right);
46+
}
47+
};
48+
```
49+
50+
正确代码
51+
```
52+
class Solution {
53+
public:
54+
long long val = LONG_MIN;
55+
bool isValidBST(TreeNode* root) {
56+
if (root == NULL) return true;
57+
bool left = isValidBST(root->left);
58+
if (val < root->val) val = root->val;// 中序遍历,这里相当于从大到小进行比较
59+
else return false;
60+
bool right = isValidBST(root->right);
61+
return left && right;
62+
}
63+
};
64+
```

problems/0225.用队列实现栈.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,5 +58,5 @@ public:
5858
}
5959
};
6060
```
61-
> 更过算法干货文章持续更新,可以微信搜索「代码随想录」第一时间围观,关注后,回复「Java」「C++」 「python」「简历模板」「数据结构与算法」等等,就可以获得我多年整理的学习资料。
61+
> 更多算法干货文章持续更新,可以微信搜索「代码随想录」第一时间围观,关注后,回复「Java」「C++」 「python」「简历模板」「数据结构与算法」等等,就可以获得我多年整理的学习资料。
6262

problems/0226.翻转二叉树.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
## 题目地址
2+
https://leetcode-cn.com/problems/invert-binary-tree/
3+
4+
## 思路
5+
6+
递归的过程,交换左右节点。
7+
8+
## C++代码
9+
10+
```
11+
class Solution {
12+
public:
13+
TreeNode* invertTree(TreeNode* root) {
14+
if (root == NULL) return root;
15+
swap(root->left, root->right);
16+
invertTree(root->left);
17+
invertTree(root->right);
18+
return root;
19+
}
20+
};
21+
```
22+
23+
> 更多算法干货文章持续更新,可以微信搜索「代码随想录」第一时间围观,关注后,回复「Java」「C++」 「python」「简历模板」「数据结构与算法」等等,就可以获得我多年整理的学习资料。

problems/0617.合并二叉树.md

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
## 题目地址
2+
https://leetcode-cn.com/problems/merge-two-binary-trees/
3+
4+
## 思路
5+
6+
四种写法,总有一款适合你,其实这道题目迭代法实现是比较困难的,大家可以试一试,是一道不错的面试进阶题目。
7+
8+
四种写法如下:
9+
10+
1. 递归修改了输入树的结构
11+
2. 递归不修改树的结构
12+
3. 递归,一波指针的操作,自己写的野路子(可以用来深度理解一下C++的指针)
13+
4. 迭代(这应该是最简单直观的迭代法代码了,一看就懂)
14+
15+
## C++代码
16+
17+
### 递归
18+
19+
修改了输入树的结构
20+
```
21+
class Solution {
22+
public:
23+
TreeNode* mergeTrees(TreeNode* t1, TreeNode* t2) {
24+
if (t1 == NULL) return t2;
25+
if (t2 == NULL) return t1;
26+
t1->val += t2->val;
27+
t1->left = mergeTrees(t1->left, t2->left);
28+
t1->right = mergeTrees(t1->right, t2->right);
29+
return t1;
30+
}
31+
};
32+
```
33+
34+
不修改输入树的结构
35+
```
36+
class Solution {
37+
public:
38+
TreeNode* mergeTrees(TreeNode* t1, TreeNode* t2) {
39+
if (t1 == NULL) return t2;
40+
if (t2 == NULL) return t1;
41+
TreeNode* root = new TreeNode(0);
42+
root->val = t1->val + t2->val;
43+
root->left = mergeTrees(t1->left, t2->left);
44+
root->right = mergeTrees(t1->right, t2->right);
45+
return root;
46+
}
47+
};
48+
```
49+
50+
一波指针的操作,自己写的野路子
51+
想要更改二叉树的值,应该传入指向指针的指针, 如果process(t1, t2);这么写的话,其实只是传入的一个int型的指针,并没有传入地址,要传入指向指针的指针才能完成对t1的修改。
52+
```
53+
class Solution {
54+
public:
55+
void process(TreeNode** t1, TreeNode** t2) {
56+
if ((*t1) == NULL && (*t2) == NULL) return;
57+
if ((*t1) != NULL && (*t2) != NULL) {
58+
(*t1)->val += (*t2)->val;
59+
}
60+
if ((*t1) == NULL && (*t2) != NULL) {
61+
*t1 = *t2;
62+
return;
63+
}
64+
if ((*t1) != NULL && (*t2) == NULL) {
65+
return;
66+
}
67+
process(&((*t1)->left), &((*t2)->left));
68+
process(&((*t1)->right), &((*t2)->right));
69+
}
70+
TreeNode* mergeTrees(TreeNode* t1, TreeNode* t2) {
71+
process(&t1, &t2);
72+
return t1;
73+
}
74+
};
75+
```
76+
### 迭代
77+
78+
这应该是最简单直观的迭代法了
79+
```
80+
class Solution {
81+
public:
82+
TreeNode* mergeTrees(TreeNode* t1, TreeNode* t2) {
83+
if (t1 == NULL) return t2;
84+
if (t2 == NULL) return t1;
85+
queue<TreeNode*> que;
86+
que.push(t1);
87+
que.push(t2);
88+
while(!que.empty()) {
89+
TreeNode* node1 = que.front(); que.pop();
90+
TreeNode* node2 = que.front(); que.pop();
91+
// 两个节点不为空,val相加
92+
if (node1 != NULL && node2 != NULL) {
93+
node1->val += node2->val;
94+
}
95+
// 如果左节点都不为空,加入队列
96+
if (node1->left != NULL && node2->left != NULL) {
97+
que.push(node1->left);
98+
que.push(node2->left);
99+
}
100+
// 如果右节点都不为空,加入队列
101+
if (node1->right != NULL && node2->right != NULL) {
102+
que.push(node1->right);
103+
que.push(node2->right);
104+
}
105+
// 当t1的左节点 为空 t2左节点不为空,就赋值过去
106+
if (node1->left == NULL && node2->left != NULL) {
107+
node1->left = node2->left;
108+
}
109+
// 当t1的右节点 为空 t2右节点不为空,就赋值过去
110+
if (node1->right == NULL && node2->right != NULL) {
111+
node1->right = node2->right;
112+
}
113+
}
114+
return t1;
115+
}
116+
};
117+
```
118+
119+
> 更多算法干货文章持续更新,可以微信搜索「代码随想录」第一时间围观,关注后,回复「Java」「C++」 「python」「简历模板」「数据结构与算法」等等,就可以获得我多年整理的学习资料。

problems/0654.最大二叉树.md

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
## 题目地址
2+
https://leetcode-cn.com/problems/maximum-binary-tree/
3+
4+
## 思路
5+
6+
典型的递归问题
7+
8+
1. 每一层要返回上一层什么内容
9+
2. 明确终止条件是什么,明确了返回内容,才知道终止条件要返回什么
10+
3. 每一层的处理条件
11+
12+
13+
## C++代码
14+
15+
```
16+
class Solution {
17+
public:
18+
TreeNode* constructMaximumBinaryTree(vector<int>& nums) {
19+
TreeNode* node = new TreeNode(0);
20+
if (nums.size() == 1) {
21+
node->val = nums[0];
22+
return node;
23+
}
24+
int maxValue = 0;
25+
int maxValueIndex = 0;
26+
for (int i = 0; i < nums.size(); i++) {
27+
if (nums[i] > maxValue) {
28+
maxValue = nums[i];
29+
maxValueIndex = i;
30+
}
31+
}
32+
node->val = maxValue;
33+
if (maxValueIndex > 0) {
34+
vector<int> newVec(nums.begin(), nums.begin() + maxValueIndex);
35+
node->left = constructMaximumBinaryTree(newVec);
36+
}
37+
if (maxValueIndex < (nums.size() - 1)) {
38+
vector<int> newVec(nums.begin() + maxValueIndex + 1, nums.end());
39+
node->right = constructMaximumBinaryTree(newVec);
40+
}
41+
return node;
42+
}
43+
};
44+
```

0 commit comments

Comments
 (0)