Skip to content

Commit e0b3bb9

Browse files
Update
1 parent 4256625 commit e0b3bb9

File tree

6 files changed

+239
-2
lines changed

6 files changed

+239
-2
lines changed

README.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# 算法面试思维导图:
22

3-
![算法面试知识大纲](https://img-blog.csdnimg.cn/20200729181420491.png)
3+
![算法面试知识大纲](https://img-blog.csdnimg.cn/2020073016244490.png)
44

55
# 算法文章精选:
66

@@ -39,7 +39,10 @@
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-
|[0101.对称二叉树](https://github.com/youngyangyang04/leetcode/blob/master/problems/0101.对称二叉树.md) ||简单|**递归** **迭代/栈**|
42+
|[0100.相同的树](https://github.com/youngyangyang04/leetcode/blob/master/problems/0100.相同的树.md) ||简单|**递归** |
43+
|[0101.对称二叉树](https://github.com/youngyangyang04/leetcode/blob/master/problems/0101.对称二叉树.md) ||简单|**递归** **迭代/队列/栈**|
44+
|[0104.二叉树的最大深度](https://github.com/youngyangyang04/leetcode/blob/master/problems/0104.二叉树的最大深度.md) ||简单|**递归** **队列/BFS**|
45+
|[0110.平衡二叉树](https://github.com/youngyangyang04/leetcode/blob/master/problems/0110.平衡二叉树.md) ||简单|**递归**|
4346
|[0142.环形链表II](https://github.com/youngyangyang04/leetcode/blob/master/problems/0142.环形链表II.md) |链表 |中等|**快慢指针/双指针**|
4447
|[0144.二叉树的前序遍历](https://github.com/youngyangyang04/leetcode/blob/master/problems/0144.二叉树的前序遍历.md) ||中等|**递归** **迭代/栈**|
4548
|[0145.二叉树的后序遍历](https://github.com/youngyangyang04/leetcode/blob/master/problems/0145.二叉树的后序遍历.md) ||困难|**递归** **迭代/栈**|

problems/0100.相同的树.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
## 题目地址
2+
https://leetcode-cn.com/problems/same-tree/
3+
4+
## 思路
5+
6+
这道题目和101 基本是一样的
7+
8+
## C++代码
9+
10+
```
11+
class Solution {
12+
public:
13+
bool compare(TreeNode* left, TreeNode* right) {
14+
if (left == NULL && right != NULL) return false;
15+
else if (left != NULL && right == NULL) return false;
16+
else if (left == NULL && right == NULL) return true;
17+
else if (left->val != right->val) return false;
18+
else return compare(left->left, right->left) && compare(left->right, right->right);
19+
20+
}
21+
bool isSameTree(TreeNode* p, TreeNode* q) {
22+
return compare(p, q);
23+
}
24+
};
25+
```
26+
> 更多算法干货文章持续更新,可以微信搜索「代码随想录」第一时间围观,关注后,回复「Java」「C++」 「python」「简历模板」「数据结构与算法」等等,就可以获得我多年整理的学习资料。

problems/0101.对称二叉树.md

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ https://leetcode-cn.com/problems/symmetric-tree/
33

44
## 思路
55

6+
这是考察二叉树基本操作的经典题目,递归的方式相对好理解一些,迭代的方法 我看大家清一色使用队列,其实使用栈也是可以的,只不过遍历的顺序不同而已,关键是要理解只要是对称比较就可以了,遍历的顺序无所谓的。
67

78
## C++代码
89

@@ -28,5 +29,64 @@ public:
2829

2930
### 迭代
3031

32+
使用队列
33+
34+
```
35+
class Solution {
36+
public:
37+
bool isSymmetric(TreeNode* root) {
38+
if (root == NULL) return true;
39+
queue<TreeNode*> que;
40+
que.push(root->left);
41+
que.push(root->right);
42+
while (!que.empty()) {
43+
TreeNode* leftNode = que.front(); que.pop();
44+
TreeNode* rightNode = que.front(); que.pop();
45+
if (!leftNode && !rightNode) {
46+
continue;
47+
}
48+
if ((!leftNode || !rightNode || (leftNode->val != rightNode->val))) {
49+
return false;
50+
}
51+
que.push(leftNode->left);
52+
que.push(rightNode->right);
53+
que.push(leftNode->right);
54+
que.push(rightNode->left);
55+
}
56+
return true;
57+
}
58+
};
59+
60+
```
61+
62+
使用栈
63+
64+
```
65+
class Solution {
66+
public:
67+
bool isSymmetric(TreeNode* root) {
68+
if (root == NULL) return true;
69+
stack<TreeNode*> st;
70+
st.push(root->left);
71+
st.push(root->right);
72+
while (!st.empty()) {
73+
TreeNode* leftNode = st.top(); st.pop();
74+
TreeNode* rightNode = st.top(); st.pop();
75+
if (!leftNode && !rightNode) {
76+
continue;
77+
}
78+
if ((!leftNode || !rightNode || (leftNode->val != rightNode->val))) {
79+
return false;
80+
}
81+
st.push(leftNode->left);
82+
st.push(rightNode->right);
83+
st.push(leftNode->right);
84+
st.push(rightNode->left);
85+
}
86+
return true;
87+
}
88+
};
89+
```
90+
3191

3292
> 更多算法干货文章持续更新,可以微信搜索「代码随想录」第一时间围观,关注后,回复「Java」「C++」 「python」「简历模板」「数据结构与算法」等等,就可以获得我多年整理的学习资料。
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
## 题目地址
2+
https://leetcode-cn.com/problems/maximum-depth-of-binary-tree/
3+
4+
## 思路
5+
6+
7+
## C++代码
8+
9+
### 递归
10+
11+
```
12+
class Solution {
13+
public:
14+
int getDepth(TreeNode* node) {
15+
if (node == NULL) return 0;
16+
return 1 + max(getDepth(node->left), getDepth(node->right));
17+
}
18+
int maxDepth(TreeNode* root) {
19+
return getDepth(root);
20+
}
21+
};
22+
```
23+
24+
### BFS
25+
26+
```
27+
class Solution {
28+
public:
29+
int maxDepth(TreeNode* root) {
30+
if (root == NULL) return 0;
31+
int depth = 0;
32+
queue<TreeNode*> que;
33+
que.push(root);
34+
while(!que.empty()) {
35+
int size = que.size(); // 必须要这么写,要固定size大小
36+
depth++;
37+
for (int i = 0; i < size; i++) {
38+
TreeNode* node = que.front();
39+
que.pop();
40+
if (node->left) que.push(node->left);
41+
if (node->right) que.push(node->right);
42+
}
43+
}
44+
return depth;
45+
}
46+
};
47+
```
48+
49+
> 更多算法干货文章持续更新,可以微信搜索「代码随想录」第一时间围观,关注后,回复「Java」「C++」 「python」「简历模板」「数据结构与算法」等等,就可以获得我多年整理的学习资料。

problems/0110.平衡二叉树.md

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
## 题目地址
2+
https://leetcode-cn.com/problems/balanced-binary-tree/
3+
4+
## 思路
5+
6+
7+
## C++代码
8+
9+
```
10+
class Solution {
11+
public:
12+
// 一开始的想法是 遍历这棵树,然后针对每个节点判断其左右子树的深度
13+
14+
// 求node为头节点二叉树的深度,并且比较起左右节点的高度差
15+
// 平衡树的条件: 左子树是平衡树,右子树是平衡树,左右子树高度相差不超过1。
16+
// 递归三部曲
17+
// 1. 明确终止条件:如果node为null
18+
// 2. 明确返回信息: 判断左子树,判断右子树
19+
// 3. 一次递归要处理的逻辑
20+
int depth(TreeNode* node) {
21+
if (node == NULL) {
22+
return 0;
23+
}
24+
int left = depth(node->left);
25+
if (left == -1) return -1;
26+
int right = depth(node->right);
27+
if (right == -1) return -1;
28+
return abs(left - right) > 1 ? -1 : 1 + max(left, right);
29+
}
30+
bool isBalanced(TreeNode* root) {
31+
return depth(root) == -1 ? false : true;
32+
}
33+
};
34+
```
35+
36+
> 更多算法干货文章持续更新,可以微信搜索「代码随想录」第一时间围观,关注后,回复「Java」「C++」 「python」「简历模板」「数据结构与算法」等等,就可以获得我多年整理的学习资料。
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
2+
## 题目地址
3+
https://leetcode-cn.com/problems/minimum-depth-of-binary-tree/
4+
5+
## 思路
6+
7+
8+
## C++代码
9+
10+
### 递归
11+
```
12+
class Solution {
13+
public:
14+
int process(TreeNode* node) {
15+
if (node == NULL) return 0;
16+
if (node->left == NULL && node->right != NULL) { // 当一个左右其中一个孩子为空的时候并不是最低点
17+
return 1 + process(node->right);
18+
}
19+
if (node->left != NULL && node->right == NULL) { // 当一个左右其中一个孩子为空的时候并不是最低点
20+
return 1 + process(node->left);
21+
}
22+
return 1 + min(process(node->left), process(node->right));
23+
}
24+
25+
int minDepth(TreeNode* root) {
26+
return process(root);
27+
}
28+
};
29+
30+
```
31+
32+
### BFS
33+
```
34+
class Solution {
35+
public:
36+
37+
int minDepth(TreeNode* root) {
38+
if (root == NULL) return 0;
39+
int depth = 0;
40+
queue<TreeNode*> que;
41+
que.push(root);
42+
while(!que.empty()) {
43+
int size = que.size(); // 必须要这么写,要固定size大小
44+
depth++;
45+
int flag = 0;
46+
for (int i = 0; i < size; i++) {
47+
TreeNode* node = que.front();
48+
que.pop();
49+
if (node->left) que.push(node->left);
50+
if (node->right) que.push(node->right);
51+
if (!node->left && !node->right) { // 当左右孩子都为空的时候,说明是最低点的一层了,退出
52+
flag = 1;
53+
break;
54+
}
55+
}
56+
if (flag == 1) break;
57+
}
58+
return depth;
59+
}
60+
};
61+
```
62+
63+
> 更多算法干货文章持续更新,可以微信搜索「代码随想录」第一时间围观,关注后,回复「Java」「C++」 「python」「简历模板」「数据结构与算法」等等,就可以获得我多年整理的学习资料。

0 commit comments

Comments
 (0)