int n, delVal; cin >> n; vector<int> nums(n); for (int i = 0; i < n; i++) { cin >> nums[i]; } cin >> delVal; TreeNode* root = nullptr; for (int i = 0; i < n; i++) { root = insert(root, nums[i]); } cout << "原始数据:"; for (int i = 0; i < n; i++) { cout << nums[i] << " "; } cout << endl; cout << "中序遍历结果:"; inorder(root); cout << endl ; TreeNode* delNode = search(root, delVal); if (delNode == nullptr) { cout << "删除的节点不存在!" << endl; } else { root = deleteNode(root, delVal); cout << "删除结点后结果:"; inorder(root); cout << endl ; } return 0; }
时间: 2024-02-10 17:19:36 浏览: 13
这段代码看起来像是二叉搜索树的相关操作,其中包括插入、中序遍历、删除节点等操作。具体来说,这段代码首先读入一个整数 n 和一个要删除的值 delVal,然后再读入 n 个整数作为二叉搜索树的节点值。接下来,程序根据这些节点值创建一个二叉搜索树,并输出原始数据和中序遍历结果。然后,程序在这个二叉搜索树中查找要删除的节点,如果节点不存在则输出一条错误信息,否则删除该节点并输出删除后的中序遍历结果。最后程序返回 0。
相关问题
往vector<int> nums里输入n个整数
好的,如果你想往vector<int> nums里输入n个整数,可以使用以下代码:
```c++
vector<int> nums;
int n;
cin >> n; // 输入n
for (int i = 0; i < n; i++) {
int x;
cin >> x; // 输入每一个整数
nums.push_back(x); // 将每个输入的整数插入到nums的末尾
}
```
这样,你就可以输入n个整数,并存储在vector<int> nums中。
#include <iostream> #include <algorithm> #include <vector> using namespace std; int main() { int n; cin >> n; vector<int> nums(n); for (int i = 0; i < n; i++) { cin >> nums[i]; } sort(nums.begin(), nums.end()); // 排序
您的代码漏掉了大括号,请在sort语句后面加上大括号。另外,您需要加上输出结果的语句。完整的代码如下:
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
int main() {
int n;
cin >> n;
vector<int> nums(n);
for (int i = 0; i < n; i++) {
cin >> nums[i];
}
sort(nums.begin(), nums.end()); // 排序
for (int i = 0; i < n; i++) {
cout << nums[i] << " ";
}
return 0;
}
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.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)