题解
2023-07-06 13:48:31
发布于:上海
271阅读
0回复
0点赞
#include <bits/stdc++.h>
using namespace std;
struct node {
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;
}
全部评论 1
不愧是大学生,牛
2023-08-18 来自 广东
0+1
2024-07-13 来自 广东
0
有帮助,赞一个