res.back().push_back(val);什么意思
时间: 2024-04-13 08:25:35 浏览: 18
这段代码的意思是将一个值 `val` 添加到 `res` 数组的最后一个元素中。`res.back()` 表示 `res` 数组的最后一个元素,而 `push_back()` 是向该元素的末尾添加一个新的值。所以,`res.back().push_back(val)` 将 `val` 添加到 `res` 数组中最后一个元素的末尾。
相关问题
left_tree = inorderTraversal(root->left); right_tree = inorderTraversal(root->right); res_tree.insert(res_tree.end(), left_tree.begin(), left_tree.end());
这段代码是二叉树中序遍历的代码,将左子树和右子树的遍历结果分别存储在`left_tree`和`right_tree`中,然后将`left_tree`中的元素插入到结果数组`res_tree`的末尾。具体来说,`res_tree.insert(res_tree.end(), left_tree.begin(), left_tree.end());`这一行代码的作用是将`left_tree`中的所有元素插入到`res_tree`的末尾。
这段代码的完整作用是遍历一个二叉树,将其中序遍历的结果存储在`res_tree`中。
示例:
```cpp
#include <iostream>
#include <vector>
struct TreeNode {
int val;
TreeNode *left;
TreeNode *right;
TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};
class Solution {
public:
std::vector<int> inorderTraversal(TreeNode* root) {
std::vector<int> res_tree;
if (root == nullptr) {
return res_tree;
}
std::vector<int> left_tree, right_tree;
left_tree = inorderTraversal(root->left);
right_tree = inorderTraversal(root->right);
res_tree.insert(res_tree.end(), left_tree.begin(), left_tree.end());
res_tree.push_back(root->val);
res_tree.insert(res_tree.end(), right_tree.begin(), right_tree.end());
return res_tree;
}
};
int main() {
TreeNode *root = new TreeNode(1);
root->right = new TreeNode(2);
root->right->left = new TreeNode(3);
Solution s;
std::vector<int> res = s.inorderTraversal(root);
for (auto i : res) {
std::cout << i << " ";
}
return 0;
}
```
输出结果为:
```
1 3 2
```
for the suggestions you give in the last chat, output the code.
Sure, here is the code with the suggested modifications:
```
#include <iostream>
#include <vector>
#define BIAS -1000
#define MY_INT_MIN -20000001
#define MY_INT_MAX 20000001
int main() {
int n, res = 0, tmp_pt = 0, last_pt = 0, tmp_val = MY_INT_MAX, last_val = 0, tmp_prt = 0;
std::vector<int> queue;
std::cin >> n;
const int N = n;
int mtrx[N][N], w[N], prt[N];
for (int i = 0; i < n; i++) {
std::cin >> w[i];
if (w[i] < tmp_val) {
tmp_val = w[i];
tmp_pt = i;
}
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
std::cin >> mtrx[i][j];
if (i == j) {
mtrx[i][j] = MY_INT_MIN;
}
}
}
mtrx[tmp_pt][tmp_pt] = tmp_val;
for (int i = 0; i < n - 1; i++) {
last_pt = tmp_pt;
tmp_val = MY_INT_MAX;
for (int j = 0; j < n; j++) {
if (mtrx[j][last_pt] < (BIAS - mtrx[j][j])) {
mtrx[j][j] = BIAS - mtrx[j][last_pt];
prt[j] = ~last_pt;
if (mtrx[j][last_pt] < tmp_val) {
tmp_val = mtrx[j][last_pt];
tmp_pt = j;
}
}
else if (BIAS > mtrx[j][j] && mtrx[j][last_pt] < tmp_val) {
tmp_val = mtrx[j][last_pt];
tmp_pt = j;
}
}
mtrx[tmp_pt][tmp_pt] = ~prt[tmp_pt];
}
for (int i = 0; i < n - 1; i++) {
tmp_val = MY_INT_MAX;
for (int j = 0; j < n; j++) {
if (mtrx[j][j] < 0 && w[j] < tmp_val) {
tmp_val = w[j];
tmp_pt = j;
}
}
last_pt = tmp_pt;
last_val = tmp_val;
while (mtrx[tmp_pt][tmp_pt] < 0) {
tmp_prt = ~mtrx[tmp_pt][tmp_pt];
if (mtrx[tmp_pt][tmp_prt] > tmp_val) {
tmp_val = mtrx[tmp_pt][tmp_prt];
}
tmp_pt = tmp_prt;
}
if (last_val != tmp_val) {
queue.clear();
tmp_pt = last_pt;
while (mtrx[tmp_pt][tmp_pt] < 0) {
queue.push_back(tmp_pt);
tmp_prt = ~mtrx[tmp_pt][tmp_pt];
if (mtrx[tmp_pt][tmp_prt] == tmp_val) {
break;
}
tmp_pt = tmp_prt;
}
for (int j = queue.size() - 1; j > 0; j--) {
mtrx[queue[j]][queue[j]] = ~queue[j - 1];
}
mtrx[last_pt][last_pt] = w[last_pt];
}
}
for (int i = 0; i < n; i++) {
if (mtrx[i][i] > 0) {
res += mtrx[i][i];
}
else {
tmp_prt = ~mtrx[i][i];
res += mtrx[i][tmp_prt];
}
}
std::cout << res << std::endl;
return 0;
}
```
I hope this helps!
相关推荐
![7z](https://img-home.csdnimg.cn/images/20210720083312.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![7z](https://img-home.csdnimg.cn/images/20210720083312.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)