题解
2025-01-26 15:27:21
发布于:广东
1阅读
0回复
0点赞
#include <bits/stdc++.h>
using namespace std;
struct Node {
int left, right;
};
Node tree[101];
void preorder(int root) {
if (root == 0) return;
cout << root << " ";
preorder(tree[root].left);
preorder(tree[root].right);
}
void inorder(int root) {
if (root == 0) return;
inorder(tree[root].left);
cout << root << " ";
inorder(tree[root].right);
}
void postorder(int root) {
if (root == 0) return;
postorder(tree[root].left);
postorder(tree[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;
tree[idx].left = left;
tree[idx].right = right;
}
int root = 1;
preorder(root);
cout << endl;
inorder(root);
cout << endl;
postorder(root);
cout << endl;
return 0;
}
这里空空如也
有帮助,赞一个