#include<iostream>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<queue>
using namespace std;
typedef struct Tree{
int data;
struct Tree* left;
struct Tree* right;
}Tree,*Bin;
int flag=1;
Bin pre_Create(){
Bin T;
int n;cin>>n;
if(n==0) T=NULL;
else{
T=(Bin)malloc(sizeof(Tree));
T->data =n;
T->left =pre_Create();
T->right =pre_Create();
}
return T;
}
void pre_Traverse(Bin T){
if(T){
if(flag==0) cout<<" ";
cout<<T->data ;
flag=0;
pre_Traverse(T->left );
pre_Traverse(T->right);
}
}
//层序创建或者层序遍历的时候,用到队列
Bin ceng_Create(){
Bin T,BT;
int data;
queue<Bin>Q;
cin>>data;
if(data==0) return NULL;
else{
BT=new Tree();
BT->data =data;
BT->left =BT->right =NULL;
Q.push(BT);
}
while(!Q.empty() ){
T=Q.front();
Q.pop();
cin>>data;
if(data==0) T->left =NULL;
else{
T->left =new Tree();
T->left ->data=data;
T->left ->left=T->left ->right=NULL;
Q.push(T->left);
}
cin>>data;
if(data==0) T->right =NULL;
else{
T->right =new Tree();
T->right ->data=data;
T->right ->left=T->right ->right=NULL;
Q.push(T->right);
}
}
return BT;
}
void ceng_Traverse(Bin BT){
queue<Bin>Q;
Bin T;
if(!BT) return;
Q.push(BT);
while(!Q.empty() ){
T=Q.front() ;
Q.pop();
cout<<T->data <<endl;
if(T->left ) Q.push(T->left );
if(T->right ) Q.push(T->right );
}
}
void test01(){
Bin T=pre_Create();
pre_Traverse(T);
}
void test02(){
Bin T=ceng_Create();
ceng_Traverse(T);
}
int main(){
// test01();
test02();
return 0;
}