文章
9
粉丝
126
获赞
8
访问
24.5k
#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);
}
...
登录后发布评论
暂无评论,来抢沙发