求调
  • 板块灌水区
  • 楼主Jimmy_Cao
  • 当前回复7
  • 已保存回复7
  • 发布时间2024/9/5 23:09
  • 上次更新2024/9/6 14:44:38
查看原帖
求调
1351065
Jimmy_Cao楼主2024/9/5 23:09

题目

#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;
}
2024/9/5 23:09
加载中...