mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2026-02-03 10:53:25 +08:00
Update
This commit is contained in:
@@ -66,8 +66,10 @@ public:
|
||||
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(); // 将空节点弹出
|
||||
|
||||
34
problems/0102.二叉树的层序遍历.md
Normal file
34
problems/0102.二叉树的层序遍历.md
Normal file
@@ -0,0 +1,34 @@
|
||||
## 题目地址
|
||||
https://leetcode-cn.com/problems/binary-tree-level-order-traversal/
|
||||
|
||||
## 思路
|
||||
|
||||
使用队列实现广度优先遍历
|
||||
|
||||
## C++代码
|
||||
|
||||
```
|
||||
class Solution {
|
||||
public:
|
||||
vector<vector<int>> levelOrder(TreeNode* root) {
|
||||
queue<TreeNode*> que;
|
||||
if (root != NULL) que.push(root);
|
||||
vector<vector<int>> result;
|
||||
while (!que.empty()) {
|
||||
int size = que.size();
|
||||
vector<int> vec;
|
||||
for (int i = 0; i < size; i++) {// 这里一定要使用固定大小size,不要使用que.size()
|
||||
TreeNode* node = que.front();
|
||||
que.pop();
|
||||
vec.push_back(node->val);
|
||||
if (node->left) que.push(node->left);
|
||||
if (node->right) que.push(node->right);
|
||||
}
|
||||
result.push_back(vec);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
};
|
||||
```
|
||||
|
||||
> 更多算法干货文章持续更新,可以微信搜索「代码随想录」第一时间围观,关注后,回复「Java」「C++」 「python」「简历模板」「数据结构与算法」等等,就可以获得我多年整理的学习资料。
|
||||
@@ -48,10 +48,10 @@ public:
|
||||
st.pop();
|
||||
if (node != NULL) result.push_back(node->val);
|
||||
else continue;
|
||||
st.push(node->left);
|
||||
st.push(node->left); // 相对于前序遍历,这更改一下入栈顺序
|
||||
st.push(node->right);
|
||||
}
|
||||
reverse(result.begin(), result.end());
|
||||
reverse(result.begin(), result.end()); // 将结果反转之后就是左右中的顺序了
|
||||
return result;
|
||||
}
|
||||
};
|
||||
@@ -63,7 +63,6 @@ public:
|
||||
```
|
||||
class Solution {
|
||||
public:
|
||||
|
||||
vector<int> postorderTraversal(TreeNode* root) {
|
||||
vector<int> result;
|
||||
stack<TreeNode*> st;
|
||||
@@ -72,10 +71,11 @@ public:
|
||||
TreeNode* node = st.top();
|
||||
if (node != NULL) {
|
||||
st.pop();
|
||||
st.push(node);
|
||||
st.push(node); // 中
|
||||
st.push(NULL);
|
||||
if (node->right) st.push(node->right);
|
||||
if (node->left) st.push(node->left);
|
||||
|
||||
if (node->right) st.push(node->right); // 右
|
||||
if (node->left) st.push(node->left); // 左
|
||||
|
||||
} else {
|
||||
st.pop();
|
||||
|
||||
32
problems/0199.二叉树的右视图.md
Normal file
32
problems/0199.二叉树的右视图.md
Normal file
@@ -0,0 +1,32 @@
|
||||
## 题目地址
|
||||
https://leetcode-cn.com/problems/binary-tree-right-side-view/
|
||||
|
||||
## 思路
|
||||
|
||||
广度优先搜索模板题目,层序遍历的时候,将每一层的最后元素放入result数组中
|
||||
|
||||
## C++代码
|
||||
|
||||
```
|
||||
class Solution {
|
||||
public:
|
||||
vector<int> rightSideView(TreeNode* root) {
|
||||
queue<TreeNode*> que;
|
||||
if (root != NULL) que.push(root);
|
||||
vector<int> result;
|
||||
while (!que.empty()) {
|
||||
int size = que.size();
|
||||
for (int i = 0; i < size; i++) {
|
||||
TreeNode* node = que.front();
|
||||
que.pop();
|
||||
if (i == (size - 1)) result.push_back(node->val);//将每一层的最后元素放入result数组中
|
||||
if (node->left) que.push(node->left);
|
||||
if (node->right) que.push(node->right);
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
};
|
||||
```
|
||||
|
||||
> 更多算法干货文章持续更新,可以微信搜索「代码随想录」第一时间围观,关注后,回复「Java」「C++」 「python」「简历模板」「数据结构与算法」等等,就可以获得我多年整理的学习资料。
|
||||
@@ -39,5 +39,5 @@ public:
|
||||
}
|
||||
};
|
||||
```
|
||||
> 更过算法干货文章持续更新,可以微信搜索「代码随想录」第一时间围观,关注后,回复「Java」「C++」 「python」「简历模板」「数据结构与算法」等等,就可以获得我多年整理的学习资料。
|
||||
> 更多算法干货文章持续更新,可以微信搜索「代码随想录」第一时间围观,关注后,回复「Java」「C++」 「python」「简历模板」「数据结构与算法」等等,就可以获得我多年整理的学习资料。
|
||||
|
||||
|
||||
46
problems/0222.完全二叉树的节点个数.md
Normal file
46
problems/0222.完全二叉树的节点个数.md
Normal file
@@ -0,0 +1,46 @@
|
||||
## 题目地址
|
||||
https://leetcode-cn.com/problems/count-complete-tree-nodes/
|
||||
|
||||
## 思路
|
||||
|
||||
递归题目
|
||||
|
||||
## C++代码
|
||||
|
||||
### 递归
|
||||
```
|
||||
class Solution {
|
||||
public:
|
||||
int countNodes(TreeNode* root) {
|
||||
if (root == NULL) return 0;
|
||||
return 1 + countNodes(root->left) + countNodes(root->right);
|
||||
}
|
||||
};
|
||||
```
|
||||
|
||||
### 迭代-广度优先
|
||||
|
||||
```
|
||||
class Solution {
|
||||
public:
|
||||
int countNodes(TreeNode* root) {
|
||||
queue<TreeNode*> que;
|
||||
if (root != NULL) que.push(root);
|
||||
int count = 0;
|
||||
int result = 0;
|
||||
while (!que.empty()) {
|
||||
int size = que.size();
|
||||
for (int i = 0; i < size; i++) {
|
||||
TreeNode* node = que.front();
|
||||
que.pop();
|
||||
result++;
|
||||
if (node->left) que.push(node->left);
|
||||
if (node->right) que.push(node->right);
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
};
|
||||
```
|
||||
|
||||
> 更多算法干货文章持续更新,可以微信搜索「代码随想录」第一时间围观,关注后,回复「Java」「C++」 「python」「简历模板」「数据结构与算法」等等,就可以获得我多年整理的学习资料。
|
||||
@@ -3,10 +3,11 @@ https://leetcode-cn.com/problems/invert-binary-tree/
|
||||
|
||||
## 思路
|
||||
|
||||
递归的过程,交换左右节点。
|
||||
写递归算法的时候,要想一想是采用前中后序那种遍历方式
|
||||
|
||||
## C++代码
|
||||
|
||||
### 递归(前序遍历)
|
||||
```
|
||||
class Solution {
|
||||
public:
|
||||
@@ -19,5 +20,24 @@ public:
|
||||
}
|
||||
};
|
||||
```
|
||||
### 迭代法(前序遍历)
|
||||
|
||||
```
|
||||
class Solution {
|
||||
public:
|
||||
TreeNode* invertTree(TreeNode* root) {
|
||||
if (root == NULL) return root;
|
||||
stack<TreeNode*> st;
|
||||
st.push(root);
|
||||
while(!st.empty()) {
|
||||
TreeNode* node = st.top();
|
||||
st.pop();
|
||||
swap(node->left, node->right);
|
||||
if(node->left) st.push(node->left);
|
||||
if(node->right) st.push(node->right);
|
||||
}
|
||||
return root;
|
||||
}
|
||||
};
|
||||
```
|
||||
> 更多算法干货文章持续更新,可以微信搜索「代码随想录」第一时间围观,关注后,回复「Java」「C++」 「python」「简历模板」「数据结构与算法」等等,就可以获得我多年整理的学习资料。
|
||||
|
||||
Reference in New Issue
Block a user