文章

9

粉丝

126

获赞

8

访问

20.5k

头像
肌肉习惯的 初始化
P1411 华中科技大学机试题
发布于2023年3月13日 17:34
阅读数 2.4k

#include <bits/stdc++.h>

using namespace std;

typedef struct node {
	int data;
	struct node *lchild,*rchild;
} node,*Tree;

void createTree (Tree &T,int x) {
	if(T==NULL){
		T=new node;
		T->data=x;T->lchild=NULL;T->rchild=NULL;//左右孩子的初始化
	}else if(x>T->data) createTree(T->rchild,x);
	else if(x<T->data) createTree(T->lchild,x);
}
void preorder (Tree T) {
	if(T!=NULL){
		cout<<T->data<<" ";
		preorder(T->lchild);
		preorder(T->rchild);
		
	}
}
void inorder (Tree T) {
	if(T!=NULL){
		inorder(T->lchild);
		cout<<T->data<<" ";
		inorder(T->rchild);
		
	}
}
void postorder (Tree T) {
	if(T!=NULL){
		postorder(T->lchild);
		postorder(T->rchild);
		cout<<T->data<<" ";
	}
}

int main() {
	int n,x;
	while(scanf("%d",&n)!=EOF){//while(cin>>n)
		Tree T=NULL; //初始化非常重要,否则报错
		while(n--){
			scanf("%d",&x);
			createTree(T,x);
		}
		...
登录查看完整内容


登录后发布评论

暂无评论,来抢沙发