mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-07 15:45:40 +08:00
Update
This commit is contained in:
@ -34,7 +34,9 @@
|
||||
|[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) |树 |中等|**递归** **栈**|
|
||||
|[0142.环形链表II](https://github.com/youngyangyang04/leetcode/blob/master/problems/0142.环形链表II.md) |链表 |中等|**快慢指针/双指针**|
|
||||
|[0144.二叉树的前序遍历](https://github.com/youngyangyang04/leetcode/blob/master/problems/0144.二叉树的前序遍历.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) |哈希表 |简单|**哈希**|
|
||||
|
28
problems/0094.二叉树的中序遍历.md
Normal file
28
problems/0094.二叉树的中序遍历.md
Normal file
@ -0,0 +1,28 @@
|
||||
## 题目地址
|
||||
https://leetcode-cn.com/problems/binary-tree-inorder-traversal/
|
||||
|
||||
## 思路
|
||||
|
||||
|
||||
## C++代码
|
||||
|
||||
递归
|
||||
```
|
||||
class Solution {
|
||||
public:
|
||||
void traversal(TreeNode* root, vector<int>& vec) {
|
||||
if (root == NULL) return;
|
||||
traversal(root->left, vec);
|
||||
vec.push_back(root->val);
|
||||
traversal(root->right, vec);
|
||||
}
|
||||
vector<int> inorderTraversal(TreeNode* root) {
|
||||
vector<int> result;
|
||||
traversal(root, result);
|
||||
return result;
|
||||
}
|
||||
};
|
||||
```
|
||||
|
||||
|
||||
> 更多算法干货文章持续更新,可以微信搜索「代码随想录」第一时间围观,关注后,回复「Java」「C++」 「python」「简历模板」「数据结构与算法」等等,就可以获得我多年整理的学习资料。
|
51
problems/0144.二叉树的前序遍历.md
Normal file
51
problems/0144.二叉树的前序遍历.md
Normal file
@ -0,0 +1,51 @@
|
||||
## 题目地址
|
||||
https://leetcode-cn.com/problems/binary-tree-preorder-traversal/
|
||||
|
||||
## 思路
|
||||
|
||||
实现树的遍历有使用递归和使用栈两种思路
|
||||
|
||||
## C++代码
|
||||
|
||||
递归
|
||||
```
|
||||
class Solution {
|
||||
public:
|
||||
void traversal(TreeNode* root, vector<int>& vec) {
|
||||
if (root == NULL) return;
|
||||
vec.push_back(root->val);
|
||||
traversal(root->left, vec);
|
||||
traversal(root->right, vec);
|
||||
}
|
||||
vector<int> preorderTraversal(TreeNode* root) {
|
||||
vector<int> result;
|
||||
traversal(root, result);
|
||||
return result;
|
||||
}
|
||||
};
|
||||
```
|
||||
|
||||
栈
|
||||
```
|
||||
class Solution {
|
||||
public:
|
||||
vector<int> preorderTraversal(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->right);
|
||||
st.push(node->left);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
};
|
||||
```
|
||||
|
||||
|
||||
|
||||
> 更过算法干货文章持续更新,可以微信搜索「代码随想录」第一时间围观,关注后,回复「Java」「C++」 「python」「简历模板」「数据结构与算法」等等,就可以获得我多年整理的学习资料。
|
Reference in New Issue
Block a user