题解
2023-07-28 14:16:23
发布于:河北
6阅读
0回复
0点赞
#include <bits/stdc++.h>
using namespace std;
struct treedot {//结构体
int left, right;
} t[1010];
void preorder(int root) {//先序遍历
if (root == 0)
return;
cout << root << " ";
preorder(t[root].left);
preorder(t[root].right);
}
void inorder(int root) {//中序遍历
if (root == 0)
return;
inorder(t[root].left);
cout << root << " ";
inorder(t[root].right);
}
void postorder(int root) {//后序遍历
if (root == 0)
return;
postorder (t[root].left);
postorder (t[root].right);
cout << root << " ";
}
int main() {
int n;
cin >> n;
for (int i = 1; i <= n; i++) {
int idx, left, right;
cin >> idx >> left >> right;
t[idx].left = left, t[idx].right = right;
}
int root = 1;
preorder(root);
cout << endl;
inorder(root);
cout << endl;
postorder(root);
return 0;
}
这里空空如也
有帮助,赞一个