密码寄了 提交的代码
提交时间:2022年4月4日 11:14 语言:C++运行时间:4ms占用内存:269K
运行状态: Accepted
题目:二叉树遍历1161

大会员可查看代码,点此开通大会员

                
                    #include<bits/stdc++.h>
using namespace std;
typedef struct node{
	char data;
	node *l,*r;
	node(char c):data(c),l(NULL),r(NULL){}
}*tree;
tree create(string s,int *loc){
	if(s[*loc]=='\0'){return NULL;}
	if(s[*loc]=='#'){
		return NULL;
	}
	tree root = new node(s[*loc]);
	if(s[*loc]!='\0'){
		(*loc)++;
		root->l=create(s,loc);
	}
	if(s[*loc]!='\0'){
		(*loc)++;
		root->r=create(s,loc);
	}
	return root;
}
void in(tree t){
	if(t!=NULL){
		in(t->l);
		cout<<t->data<<" ";
		in(t->r);
	}
}
int main(){
	string s;
	while(cin>>s){
		int loc=0;
		tree root;
		root= create(s, &loc);
		in(root);
		cout << endl;
	}
	return 0;
}