题目
#include <bits/stdc++.h>
using namespace std;
struct node {
int data;
node *Lchild, *Rchild;
} *root;
int n, t;
void build(node *father) {
node *p;
int Left, Right;
cin >> Left >> Right;
if (Left) {
p->data = Left;
p->Lchild = nullptr;
p->Rchild = nullptr;
father->Lchild = p;
}
if (Right) {
p->data = Right;
p->Lchild = nullptr;
p->Rchild = nullptr;
father->Rchild = p;
}
p->data = ++t;
p->Lchild = nullptr;
p->Rchild = nullptr;
cout << p->data << ' ' << p->Lchild << ' ' << p->Rchild << endl;
build(p);
}
void pre_order(node *father) {
cout << father->data << ' ';
if (father->Lchild) pre_order(father->Lchild);
if (father->Rchild) pre_order(father->Rchild);
}
void in_order(node *father) {
if (father->Lchild) in_order(father->Lchild);
cout << father->data << ' ';
if (father->Rchild) in_order(father->Rchild);
}
void post_order(node *father) {
if (father->Lchild) post_order(father->Lchild);
if (father->Rchild) post_order(father->Rchild);
cout << father->data << ' ';
}
int main() {
cin >> n;
root = new node;
root->data = ++t;
root->Lchild = nullptr;
root->Rchild = nullptr;
build(root);
pre_order(root); cout << endl;
in_order(root); cout << endl;
post_order(root); cout << endl;
return 0;
}