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

|
||||
|
||||
# 算法文章精选:
|
||||
|
||||
* [究竟什么是时间复杂度,怎么求时间复杂度,看这一篇就够了](https://mp.weixin.qq.com/s/lYL9TSxLqCeFXIdjt4dcIw)
|
||||
@ -34,9 +38,11 @@
|
||||
|[0053.最大子序和](https://github.com/youngyangyang04/leetcode/blob/master/problems/0053.最大子序和.md) |数组 |简单|**暴力** **贪心** 动态规划 分治|
|
||||
|[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) |树 |中等|**递归** **栈**|
|
||||
|[0094.二叉树的中序遍历](https://github.com/youngyangyang04/leetcode/blob/master/problems/0094.二叉树的中序遍历.md) |树 |中等|**递归** **迭代/栈**|
|
||||
|[0101.对称二叉树](https://github.com/youngyangyang04/leetcode/blob/master/problems/0101.对称二叉树.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) |树 |中等|**递归** **栈**|
|
||||
|[0144.二叉树的前序遍历](https://github.com/youngyangyang04/leetcode/blob/master/problems/0144.二叉树的前序遍历.md) |树 |中等|**递归** **迭代/栈**|
|
||||
|[0145.二叉树的后序遍历](https://github.com/youngyangyang04/leetcode/blob/master/problems/ 0145.二叉树的后序遍历.md) |树 |困难|**递归** **迭代/栈**|
|
||||
|[0151.翻转字符串里的单词](https://github.com/youngyangyang04/leetcode/blob/master/problems/0151.翻转字符串里的单词.md) |字符串 |中等|**模拟/双指针**|
|
||||
|[0155.最小栈](https://github.com/youngyangyang04/leetcode/blob/master/problems/0155.最小栈.md) |栈 |简单|**栈**|
|
||||
|[0202.快乐数](https://github.com/youngyangyang04/leetcode/blob/master/problems/0202.快乐数.md) |哈希表 |简单|**哈希**|
|
||||
|
@ -3,10 +3,11 @@ https://leetcode-cn.com/problems/binary-tree-inorder-traversal/
|
||||
|
||||
## 思路
|
||||
|
||||
题号:94,144,145 一起做一下
|
||||
|
||||
## C++代码
|
||||
|
||||
递归
|
||||
### 递归
|
||||
```
|
||||
class Solution {
|
||||
public:
|
||||
@ -24,5 +25,58 @@ public:
|
||||
};
|
||||
```
|
||||
|
||||
### 栈
|
||||
```
|
||||
class Solution {
|
||||
public:
|
||||
vector<int> inorderTraversal(TreeNode* root) {
|
||||
vector<int> result;
|
||||
stack<TreeNode*> st;
|
||||
TreeNode* cur = root;
|
||||
while (cur != NULL || !st.empty()) {
|
||||
if (cur != NULL) {
|
||||
st.push(cur);
|
||||
cur = cur->left;
|
||||
} else {
|
||||
cur = st.top();
|
||||
st.pop();
|
||||
result.push_back(cur->val);
|
||||
cur = cur->right;
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
};
|
||||
```
|
||||
|
||||
### 栈 通用模板
|
||||
|
||||
```
|
||||
class Solution {
|
||||
public:
|
||||
vector<int> inorderTraversal(TreeNode* root) {
|
||||
vector<int> result;
|
||||
stack<TreeNode*> st;
|
||||
if (root != NULL) st.push(root);
|
||||
while (!st.empty()) {
|
||||
TreeNode* node = st.top();
|
||||
if (node != NULL) {
|
||||
st.pop(); // 将该节点弹出,避免重复操作,下面再将右中左节点添加到栈中
|
||||
if (node->right) st.push(node->right); // 添加右节点
|
||||
st.push(node); // 添加中节点
|
||||
st.push(NULL); // 中节点访问过,但是还没有处理,需要做一下标记。
|
||||
if (node->left) st.push(node->left); // 添加左节点
|
||||
} else {
|
||||
st.pop(); // 将空节点弹出
|
||||
node = st.top(); // 重新取出栈中元素
|
||||
st.pop();
|
||||
result.push_back(node->val); // 加入到数组中
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
};
|
||||
```
|
||||
|
||||
|
||||
> 更多算法干货文章持续更新,可以微信搜索「代码随想录」第一时间围观,关注后,回复「Java」「C++」 「python」「简历模板」「数据结构与算法」等等,就可以获得我多年整理的学习资料。
|
||||
|
32
problems/0101.对称二叉树.md
Normal file
32
problems/0101.对称二叉树.md
Normal file
@ -0,0 +1,32 @@
|
||||
## 题目地址
|
||||
https://leetcode-cn.com/problems/symmetric-tree/
|
||||
|
||||
## 思路
|
||||
|
||||
|
||||
## 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->right) && compare(left->right, right->left);
|
||||
|
||||
}
|
||||
bool isSymmetric(TreeNode* root) {
|
||||
if (root == NULL) return true;
|
||||
return compare(root->left, root->right);
|
||||
}
|
||||
};
|
||||
```
|
||||
|
||||
### 迭代
|
||||
|
||||
|
||||
> 更多算法干货文章持续更新,可以微信搜索「代码随想录」第一时间围观,关注后,回复「Java」「C++」 「python」「简历模板」「数据结构与算法」等等,就可以获得我多年整理的学习资料。
|
@ -7,7 +7,7 @@ https://leetcode-cn.com/problems/binary-tree-preorder-traversal/
|
||||
|
||||
## C++代码
|
||||
|
||||
递归
|
||||
### 递归
|
||||
```
|
||||
class Solution {
|
||||
public:
|
||||
@ -25,7 +25,7 @@ public:
|
||||
};
|
||||
```
|
||||
|
||||
栈
|
||||
### 栈
|
||||
```
|
||||
class Solution {
|
||||
public:
|
||||
@ -46,6 +46,34 @@ public:
|
||||
};
|
||||
```
|
||||
|
||||
### 栈 通用模板
|
||||
详细代码注释看 [0094.二叉树的中序遍历](https://github.com/youngyangyang04/leetcode/blob/master/problems/0094.二叉树的中序遍历.md)
|
||||
```
|
||||
class Solution {
|
||||
public:
|
||||
vector<int> preorderTraversal(TreeNode* root) {
|
||||
vector<int> result;
|
||||
stack<TreeNode*> st;
|
||||
if (root != NULL) st.push(root);
|
||||
while (!st.empty()) {
|
||||
TreeNode* node = st.top();
|
||||
if (node != NULL) {
|
||||
st.pop();
|
||||
if (node->right) st.push(node->right);
|
||||
if (node->left) st.push(node->left);
|
||||
st.push(node);
|
||||
st.push(NULL);
|
||||
} else {
|
||||
st.pop();
|
||||
node = st.top();
|
||||
st.pop();
|
||||
result.push_back(node->val);
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
};
|
||||
```
|
||||
|
||||
|
||||
> 更过算法干货文章持续更新,可以微信搜索「代码随想录」第一时间围观,关注后,回复「Java」「C++」 「python」「简历模板」「数据结构与算法」等等,就可以获得我多年整理的学习资料。
|
||||
> 更多算法干货文章持续更新,可以微信搜索「代码随想录」第一时间围观,关注后,回复「Java」「C++」 「python」「简历模板」「数据结构与算法」等等,就可以获得我多年整理的学习资料。
|
||||
|
88
problems/0145.二叉树的后序遍历.md
Normal file
88
problems/0145.二叉树的后序遍历.md
Normal file
@ -0,0 +1,88 @@
|
||||
## 题目地址
|
||||
https://leetcode-cn.com/problems/binary-tree-postorder-traversal/
|
||||
|
||||
## 思路
|
||||
|
||||
题号:94,144,145 一起做一下
|
||||
|
||||
## C++代码
|
||||
|
||||
### 递归
|
||||
|
||||
```
|
||||
class Solution {
|
||||
public:
|
||||
void traversal(TreeNode* root, vector<int>& vec) {
|
||||
if (root == NULL) return;
|
||||
traversal(root->left, vec);
|
||||
traversal(root->right, vec);
|
||||
vec.push_back(root->val);
|
||||
}
|
||||
vector<int> postorderTraversal(TreeNode* root) {
|
||||
vector<int> result;
|
||||
traversal(root, result);
|
||||
return result;
|
||||
|
||||
}
|
||||
};
|
||||
```
|
||||
|
||||
### 栈
|
||||
|
||||
先中右左遍历,然后再反转
|
||||
|
||||
```
|
||||
class Solution {
|
||||
public:
|
||||
|
||||
vector<int> postorderTraversal(TreeNode* root) {
|
||||
stack<TreeNode*> st;
|
||||
vector<int> result;
|
||||
st.push(root);
|
||||
while (!st.empty()) {
|
||||
TreeNode* node = st.top();
|
||||
st.pop();
|
||||
if (node != NULL) result.push_back(node->val);
|
||||
else continue;
|
||||
st.push(node->left);
|
||||
st.push(node->right);
|
||||
}
|
||||
reverse(result.begin(), result.end());
|
||||
return result;
|
||||
}
|
||||
};
|
||||
```
|
||||
### 栈 通用模板
|
||||
|
||||
详细代码注释看 [0094.二叉树的中序遍历](https://github.com/youngyangyang04/leetcode/blob/master/problems/0094.二叉树的中序遍历.md)
|
||||
|
||||
```
|
||||
class Solution {
|
||||
public:
|
||||
|
||||
vector<int> postorderTraversal(TreeNode* root) {
|
||||
vector<int> result;
|
||||
stack<TreeNode*> st;
|
||||
if (root != NULL) st.push(root);
|
||||
while (!st.empty()) {
|
||||
TreeNode* node = st.top();
|
||||
if (node != NULL) {
|
||||
st.pop();
|
||||
st.push(node);
|
||||
st.push(NULL);
|
||||
if (node->right) st.push(node->right);
|
||||
if (node->left) st.push(node->left);
|
||||
|
||||
} else {
|
||||
st.pop();
|
||||
node = st.top();
|
||||
st.pop();
|
||||
result.push_back(node->val);
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
};
|
||||
```
|
||||
|
||||
> 更多算法干货文章持续更新,可以微信搜索「代码随想录」第一时间围观,关注后,回复「Java」「C++」 「python」「简历模板」「数据结构与算法」等等,就可以获得我多年整理的学习资料。
|
Reference in New Issue
Block a user