AcWing 1256. 扩展二叉树
原题链接
简单
作者:
浅唱笑竹神易
,
2024-10-05 21:21:24
,
所有人可见
,
阅读 2
#include<iostream>
#include<cstdio>
#include<string>
using namespace std;
string datax;
struct BTNode{
char val;
BTNode *l,*r;
};
BTNode *ans;
int build(BTNode *p,int l,int isleft){
if(l>datax.length()-1||datax[l]=='.')return l+1;
BTNode *temp=(BTNode*)malloc(sizeof(BTNode));
temp->val=datax[l];
if(isleft==1){
p->l=temp;
}
else {
p->r=temp;
}
int x=build(temp,l+1,1);
return build(temp,x,0);
}
void midorder(BTNode *p){
if(p==NULL)return ;
midorder(p->l);
cout<<p->val;
midorder(p->r);
}
void lastorder(BTNode*p){
if(p==NULL)return ;
lastorder(p->l);
lastorder(p->r);
cout<<p->val;
}
int main(){
getline(cin,datax);
ans=(BTNode*)malloc(sizeof(BTNode));
build(ans,0,1);
midorder(ans->l);
cout<<endl;
lastorder(ans->l);
return 0;
}