mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-06 23:28:29 +08:00
Update
This commit is contained in:
@ -1,6 +1,6 @@
|
||||
# 算法面试思维导图:
|
||||
|
||||

|
||||

|
||||
|
||||
# 算法文章精选:
|
||||
|
||||
@ -39,7 +39,10 @@
|
||||
|[0059.螺旋矩阵II](https://github.com/youngyangyang04/leetcode/blob/master/problems/0059.螺旋矩阵II.md) |数组 |中等|**模拟**|
|
||||
|[0083.删除排序链表中的重复元素](https://github.com/youngyangyang04/leetcode/blob/master/problems/0083.删除排序链表中的重复元素.md) |链表 |简单|**模拟**|
|
||||
|[0094.二叉树的中序遍历](https://github.com/youngyangyang04/leetcode/blob/master/problems/0094.二叉树的中序遍历.md) |树 |中等|**递归** **迭代/栈**|
|
||||
|[0101.对称二叉树](https://github.com/youngyangyang04/leetcode/blob/master/problems/0101.对称二叉树.md) |树 |简单|**递归** **迭代/栈**|
|
||||
|[0100.相同的树](https://github.com/youngyangyang04/leetcode/blob/master/problems/0100.相同的树.md) |树 |简单|**递归** |
|
||||
|[0101.对称二叉树](https://github.com/youngyangyang04/leetcode/blob/master/problems/0101.对称二叉树.md) |树 |简单|**递归** **迭代/队列/栈**|
|
||||
|[0104.二叉树的最大深度](https://github.com/youngyangyang04/leetcode/blob/master/problems/0104.二叉树的最大深度.md) |树 |简单|**递归** **队列/BFS**|
|
||||
|[0110.平衡二叉树](https://github.com/youngyangyang04/leetcode/blob/master/problems/0110.平衡二叉树.md) |树 |简单|**递归**|
|
||||
|[0142.环形链表II](https://github.com/youngyangyang04/leetcode/blob/master/problems/0142.环形链表II.md) |链表 |中等|**快慢指针/双指针**|
|
||||
|[0144.二叉树的前序遍历](https://github.com/youngyangyang04/leetcode/blob/master/problems/0144.二叉树的前序遍历.md) |树 |中等|**递归** **迭代/栈**|
|
||||
|[0145.二叉树的后序遍历](https://github.com/youngyangyang04/leetcode/blob/master/problems/0145.二叉树的后序遍历.md) |树 |困难|**递归** **迭代/栈**|
|
||||
|
26
problems/0100.相同的树.md
Normal file
26
problems/0100.相同的树.md
Normal file
@ -0,0 +1,26 @@
|
||||
## 题目地址
|
||||
https://leetcode-cn.com/problems/same-tree/
|
||||
|
||||
## 思路
|
||||
|
||||
这道题目和101 基本是一样的
|
||||
|
||||
## C++代码
|
||||
|
||||
```
|
||||
class Solution {
|
||||
public:
|
||||
bool compare(TreeNode* left, TreeNode* right) {
|
||||
if (left == NULL && right != NULL) return false;
|
||||
else if (left != NULL && right == NULL) return false;
|
||||
else if (left == NULL && right == NULL) return true;
|
||||
else if (left->val != right->val) return false;
|
||||
else return compare(left->left, right->left) && compare(left->right, right->right);
|
||||
|
||||
}
|
||||
bool isSameTree(TreeNode* p, TreeNode* q) {
|
||||
return compare(p, q);
|
||||
}
|
||||
};
|
||||
```
|
||||
> 更多算法干货文章持续更新,可以微信搜索「代码随想录」第一时间围观,关注后,回复「Java」「C++」 「python」「简历模板」「数据结构与算法」等等,就可以获得我多年整理的学习资料。
|
@ -3,6 +3,7 @@ https://leetcode-cn.com/problems/symmetric-tree/
|
||||
|
||||
## 思路
|
||||
|
||||
这是考察二叉树基本操作的经典题目,递归的方式相对好理解一些,迭代的方法 我看大家清一色使用队列,其实使用栈也是可以的,只不过遍历的顺序不同而已,关键是要理解只要是对称比较就可以了,遍历的顺序无所谓的。
|
||||
|
||||
## C++代码
|
||||
|
||||
@ -28,5 +29,64 @@ public:
|
||||
|
||||
### 迭代
|
||||
|
||||
使用队列
|
||||
|
||||
```
|
||||
class Solution {
|
||||
public:
|
||||
bool isSymmetric(TreeNode* root) {
|
||||
if (root == NULL) return true;
|
||||
queue<TreeNode*> que;
|
||||
que.push(root->left);
|
||||
que.push(root->right);
|
||||
while (!que.empty()) {
|
||||
TreeNode* leftNode = que.front(); que.pop();
|
||||
TreeNode* rightNode = que.front(); que.pop();
|
||||
if (!leftNode && !rightNode) {
|
||||
continue;
|
||||
}
|
||||
if ((!leftNode || !rightNode || (leftNode->val != rightNode->val))) {
|
||||
return false;
|
||||
}
|
||||
que.push(leftNode->left);
|
||||
que.push(rightNode->right);
|
||||
que.push(leftNode->right);
|
||||
que.push(rightNode->left);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
};
|
||||
|
||||
```
|
||||
|
||||
使用栈
|
||||
|
||||
```
|
||||
class Solution {
|
||||
public:
|
||||
bool isSymmetric(TreeNode* root) {
|
||||
if (root == NULL) return true;
|
||||
stack<TreeNode*> st;
|
||||
st.push(root->left);
|
||||
st.push(root->right);
|
||||
while (!st.empty()) {
|
||||
TreeNode* leftNode = st.top(); st.pop();
|
||||
TreeNode* rightNode = st.top(); st.pop();
|
||||
if (!leftNode && !rightNode) {
|
||||
continue;
|
||||
}
|
||||
if ((!leftNode || !rightNode || (leftNode->val != rightNode->val))) {
|
||||
return false;
|
||||
}
|
||||
st.push(leftNode->left);
|
||||
st.push(rightNode->right);
|
||||
st.push(leftNode->right);
|
||||
st.push(rightNode->left);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
};
|
||||
```
|
||||
|
||||
|
||||
> 更多算法干货文章持续更新,可以微信搜索「代码随想录」第一时间围观,关注后,回复「Java」「C++」 「python」「简历模板」「数据结构与算法」等等,就可以获得我多年整理的学习资料。
|
||||
|
49
problems/0104.二叉树的最大深度.md
Normal file
49
problems/0104.二叉树的最大深度.md
Normal file
@ -0,0 +1,49 @@
|
||||
## 题目地址
|
||||
https://leetcode-cn.com/problems/maximum-depth-of-binary-tree/
|
||||
|
||||
## 思路
|
||||
|
||||
|
||||
## C++代码
|
||||
|
||||
### 递归
|
||||
|
||||
```
|
||||
class Solution {
|
||||
public:
|
||||
int getDepth(TreeNode* node) {
|
||||
if (node == NULL) return 0;
|
||||
return 1 + max(getDepth(node->left), getDepth(node->right));
|
||||
}
|
||||
int maxDepth(TreeNode* root) {
|
||||
return getDepth(root);
|
||||
}
|
||||
};
|
||||
```
|
||||
|
||||
### BFS
|
||||
|
||||
```
|
||||
class Solution {
|
||||
public:
|
||||
int maxDepth(TreeNode* root) {
|
||||
if (root == NULL) return 0;
|
||||
int depth = 0;
|
||||
queue<TreeNode*> que;
|
||||
que.push(root);
|
||||
while(!que.empty()) {
|
||||
int size = que.size(); // 必须要这么写,要固定size大小
|
||||
depth++;
|
||||
for (int i = 0; i < size; i++) {
|
||||
TreeNode* node = que.front();
|
||||
que.pop();
|
||||
if (node->left) que.push(node->left);
|
||||
if (node->right) que.push(node->right);
|
||||
}
|
||||
}
|
||||
return depth;
|
||||
}
|
||||
};
|
||||
```
|
||||
|
||||
> 更多算法干货文章持续更新,可以微信搜索「代码随想录」第一时间围观,关注后,回复「Java」「C++」 「python」「简历模板」「数据结构与算法」等等,就可以获得我多年整理的学习资料。
|
36
problems/0110.平衡二叉树.md
Normal file
36
problems/0110.平衡二叉树.md
Normal file
@ -0,0 +1,36 @@
|
||||
## 题目地址
|
||||
https://leetcode-cn.com/problems/balanced-binary-tree/
|
||||
|
||||
## 思路
|
||||
|
||||
|
||||
## C++代码
|
||||
|
||||
```
|
||||
class Solution {
|
||||
public:
|
||||
// 一开始的想法是 遍历这棵树,然后针对每个节点判断其左右子树的深度
|
||||
|
||||
// 求node为头节点二叉树的深度,并且比较起左右节点的高度差
|
||||
// 平衡树的条件: 左子树是平衡树,右子树是平衡树,左右子树高度相差不超过1。
|
||||
// 递归三部曲
|
||||
// 1. 明确终止条件:如果node为null
|
||||
// 2. 明确返回信息: 判断左子树,判断右子树
|
||||
// 3. 一次递归要处理的逻辑
|
||||
int depth(TreeNode* node) {
|
||||
if (node == NULL) {
|
||||
return 0;
|
||||
}
|
||||
int left = depth(node->left);
|
||||
if (left == -1) return -1;
|
||||
int right = depth(node->right);
|
||||
if (right == -1) return -1;
|
||||
return abs(left - right) > 1 ? -1 : 1 + max(left, right);
|
||||
}
|
||||
bool isBalanced(TreeNode* root) {
|
||||
return depth(root) == -1 ? false : true;
|
||||
}
|
||||
};
|
||||
```
|
||||
|
||||
> 更多算法干货文章持续更新,可以微信搜索「代码随想录」第一时间围观,关注后,回复「Java」「C++」 「python」「简历模板」「数据结构与算法」等等,就可以获得我多年整理的学习资料。
|
63
problems/0111.二叉树的最小深度.md
Normal file
63
problems/0111.二叉树的最小深度.md
Normal file
@ -0,0 +1,63 @@
|
||||
|
||||
## 题目地址
|
||||
https://leetcode-cn.com/problems/minimum-depth-of-binary-tree/
|
||||
|
||||
## 思路
|
||||
|
||||
|
||||
## C++代码
|
||||
|
||||
### 递归
|
||||
```
|
||||
class Solution {
|
||||
public:
|
||||
int process(TreeNode* node) {
|
||||
if (node == NULL) return 0;
|
||||
if (node->left == NULL && node->right != NULL) { // 当一个左右其中一个孩子为空的时候并不是最低点
|
||||
return 1 + process(node->right);
|
||||
}
|
||||
if (node->left != NULL && node->right == NULL) { // 当一个左右其中一个孩子为空的时候并不是最低点
|
||||
return 1 + process(node->left);
|
||||
}
|
||||
return 1 + min(process(node->left), process(node->right));
|
||||
}
|
||||
|
||||
int minDepth(TreeNode* root) {
|
||||
return process(root);
|
||||
}
|
||||
};
|
||||
|
||||
```
|
||||
|
||||
### BFS
|
||||
```
|
||||
class Solution {
|
||||
public:
|
||||
|
||||
int minDepth(TreeNode* root) {
|
||||
if (root == NULL) return 0;
|
||||
int depth = 0;
|
||||
queue<TreeNode*> que;
|
||||
que.push(root);
|
||||
while(!que.empty()) {
|
||||
int size = que.size(); // 必须要这么写,要固定size大小
|
||||
depth++;
|
||||
int flag = 0;
|
||||
for (int i = 0; i < size; i++) {
|
||||
TreeNode* node = que.front();
|
||||
que.pop();
|
||||
if (node->left) que.push(node->left);
|
||||
if (node->right) que.push(node->right);
|
||||
if (!node->left && !node->right) { // 当左右孩子都为空的时候,说明是最低点的一层了,退出
|
||||
flag = 1;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (flag == 1) break;
|
||||
}
|
||||
return depth;
|
||||
}
|
||||
};
|
||||
```
|
||||
|
||||
> 更多算法干货文章持续更新,可以微信搜索「代码随想录」第一时间围观,关注后,回复「Java」「C++」 「python」「简历模板」「数据结构与算法」等等,就可以获得我多年整理的学习资料。
|
Reference in New Issue
Block a user