#include<iostream>using namespace std;struct TreeNode{ int val; TreeNode* left; TreeNode* right; TreeNode(int v): val(v), left(NULL), right(NULL) {}};TreeNode* buildTree(){ int val; cin >> val; if(val == -1) // 空节点 return NULL; TreeNode* root = new TreeNode(val); root->left = buildTree(); root->right = buildTree(); return root;}void inorderTraversal(TreeNode* root){ if(root == NULL) return; inorderTraversal(root->left); // 中序遍历左子树 cout << root->val << " "; // 输出当前节点 inorderTraversal(root->right); // 中序遍历右子树}int main(){ cout << "请输入二叉树的先序遍历序列(空节点用-1表示):" << endl; TreeNode* root = buildTree(); cout << "中序遍历结果为:" << endl; inorderTraversal(root); return 0;}用后序遍历搞二叉树
时间: 2024-01-26 09:03:19 浏览: 59
可以将上述代码中的inorderTraversal函数改为postorderTraversal函数,实现后序遍历输出即可。具体修改如下:
```C++
void postorderTraversal(TreeNode* root){
if(root == NULL)
return;
postorderTraversal(root->left); // 后序遍历左子树
postorderTraversal(root->right); // 后序遍历右子树
cout << root->val << " "; // 输出当前节点
}
```
然后在主函数中调用postorderTraversal函数即可输出二叉树的后序遍历结果:
```C++
int main(){
cout << "请输入二叉树的先序遍历序列(空节点用-1表示):" << endl;
TreeNode* root = buildTree();
cout << "后序遍历结果为:" << endl;
postorderTraversal(root);
return 0;
}
```
相关问题
#include<iostream>using namespace std;struct TreeNode{ int val; TreeNode* left; TreeNode* right; TreeNode(int v): val(v), left(NULL), right(NULL) {}};TreeNode* buildTree(){ int val; cin >> val; if(val == -1) // 空节点 return NULL; TreeNode* root = new TreeNode(val); root->left = buildTree(); root->right = buildTree(); return root;}int main(){ cout << "请输入二叉树的先序遍历序列(空节点用-1表示):" << endl; TreeNode* root = buildTree(); return 0;}用层次遍历该二叉树
可以使用队列来实现二叉树的层次遍历。具体步骤如下:
1. 将根节点放入队列中。
2. 当队列不为空时,取出队首节点,输出其值,并将其左右子节点(如果存在)放入队列中。
3. 重复第2步,直到队列为空。
下面是代码实现:
```c++
#include<iostream>
#include<queue>
using namespace std;
struct TreeNode{
int val;
TreeNode* left;
TreeNode* right;
TreeNode(int v): val(v), left(NULL), right(NULL) {}
};
TreeNode* buildTree(){
int val;
cin >> val;
if(val == -1) // 空节点
return NULL;
TreeNode* root = new TreeNode(val);
root->left = buildTree();
root->right = buildTree();
return root;
}
void levelOrder(TreeNode* root){
if(root == NULL)
return;
queue<TreeNode*> q;
q.push(root);
while(!q.empty()){
TreeNode* cur = q.front();
q.pop();
cout << cur->val << " ";
if(cur->left)
q.push(cur->left);
if(cur->right)
q.push(cur->right);
}
}
int main(){
cout << "请输入二叉树的先序遍历序列(空节点用-1表示):" << endl;
TreeNode* root = buildTree();
cout << "层次遍历结果为:" << endl;
levelOrder(root);
return 0;
}
```
如果输入的二叉树是这样的:
```
1
2
-1
-1
3
4
-1
-1
5
-1
-1
```
则输出的层次遍历结果为:
```
1 2 3 4 5
```
#include <iostream> #include <queue> #include<string> using namespace std; struct TreeNode { int val; TreeNode* left; TreeNode* right; TreeNode(int x) : val(x), left(NULL), right(NULL) {} }; // 递归实现先序遍历 void preorderTraversal(TreeNode* root) { if (root) { cout << root->val << " "; preorderTraversal(root->left); preorderTraversal(root->right); } } // 非递归实现层次遍历 void levelOrderTraversal(TreeNode* root) { if (!root) { return; } queue<TreeNode*> q; q.push(root); while (!q.empty()) { TreeNode* cur = q.front(); q.pop(); cout << cur->val << " "; if (cur->left) { q.push(cur->left); } if (cur->right) { q.push(cur->right); } } } int main() { // 建立二叉树 TreeNode* root = new TreeNode(65); root->left = new TreeNode(66); root->right = new TreeNode(67); root->left->left = new TreeNode(68); root->left->right = new TreeNode(69); root->left->left->left = new TreeNode(72); root->left->right->right = new TreeNode(73); root->right->left = new TreeNode(70); root->right->right = new TreeNode(71); root->right->left->left = new TreeNode(74); // 先序遍历 cout << "Preorder Traversal: "; preorderTraversal(root); cout << endl; // 层次遍历 cout << "Level Order Traversal: "; levelOrderTraversal(root); cout << endl; return 0; }怎么将代码内节点的数字改成字母
要将节点的数字改成字母,可以将 TreeNode 结构体中的 val 成员变量改为 string 类型,并将节点的值赋为相应的字母。例如,将节点的数字 65 改为字母 A,将节点的数字 66 改为字母 B,以此类推,可以将代码修改如下:
```
#include <iostream>
#include <queue>
#include <string>
using namespace std;
struct TreeNode {
string val; // 将节点的值从 int 类型改为 string 类型
TreeNode* left;
TreeNode* right;
TreeNode(string s) : val(s), left(NULL), right(NULL) {} // 将节点的值初始化为字符串类型
};
// 递归实现先序遍历
void preorderTraversal(TreeNode* root) {
if (root) {
cout << root->val << " ";
preorderTraversal(root->left);
preorderTraversal(root->right);
}
}
// 非递归实现层次遍历
void levelOrderTraversal(TreeNode* root) {
if (!root) {
return;
}
queue<TreeNode*> q;
q.push(root);
while (!q.empty()) {
TreeNode* cur = q.front();
q.pop();
cout << cur->val << " ";
if (cur->left) {
q.push(cur->left);
}
if (cur->right) {
q.push(cur->right);
}
}
}
int main() {
// 建立二叉树
TreeNode* root = new TreeNode("A"); // 将节点的值从数字改为字母
root->left = new TreeNode("B");
root->right = new TreeNode("C");
root->left->left = new TreeNode("D");
root->left->right = new TreeNode("E");
root->left->left->left = new TreeNode("H");
root->left->right->right = new TreeNode("I");
root->right->left = new TreeNode("F");
root->right->right = new TreeNode("G");
root->right->left->left = new TreeNode("J");
// 先序遍历
cout << "Preorder Traversal: ";
preorderTraversal(root);
cout << endl;
// 层次遍历
cout << "Level Order Traversal: ";
levelOrderTraversal(root);
cout << endl;
return 0;
}
```
这样就可以将节点的数字改成相应的字母了。
阅读全文