#include<algorithm>
#include<set>
#include<vector>
#include<queue>
#include<cmath>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<set>
#include<vector>
#include<queue>
#include<cmath>
#include<cstdio>
#include<map>
#include<stack>
#include<string>
using namespace std;
#define sfi(i) scanf("%d",&i)
#define pri(i) printf("%d\n",i)
#define sff(i) scanf("%lf",&i)
#define ll long long
#define mem(x,y) memset(x,y,sizeof(x))
#define INF 0x3f3f3f3f
#define eps 1e-6
#define PI acos(-1)
#define lowbit(x) ((x)&(-x))
#define zero(x) (((x)>0?(x):-(x))<eps)
#define fl() printf("flag\n")
#define MOD(x) ((x%mod)+mod)%mod
#define FASTIO ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
ll gcd(ll a,ll b){while(b^=a^=b^=a%=b);return a;}
const int maxn=2e5+9;
const int mod=1e9+7;
template <class T>
inline void sc(T &ret)
{
char c;
ret = 0;
while ((c = getchar()) < '0' || c > '9');
while (c >= '0' && c <= '9')
{
ret = ret * 10 + (c - '0'), c = getchar();
}
}
char ch;
char s[maxn];
typedef struct BiNode
{
char c;
bool fl,fr;
struct BiNode *lch,*rch;
}BiNode,*BiTree;
void CreateBiTree(BiTree &T)
{
cin>>ch;
if(ch=='#') T=NULL;
else
{
T=new BiNode;
T->c=ch;
CreateBiTree(T->lch);
CreateBiTree(T->rch);
}
}
void CreateBiTree_Stack(BiTree &T,string s)
{
int len=s.length();
stack<BiTree>st;
T=new BiNode;
T->c=s[0];
T->fl=false;
T->fr=false;
T->lch=NULL;
T->rch=NULL;
st.push(T);
for(int i=1;i<len;i++)
{
//cout<<st.top()->c<<endl;
if(s[i]!='#')
{
BiTree tmp=new BiNode;
tmp->c=s[i];
tmp->fl=false;
tmp->fr=false;
tmp->lch=NULL;
tmp->rch=NULL;
if(!st.top()->fl)
{
st.top()->lch=tmp;
st.top()->fl=true;
}
else
{
st.top()->rch=tmp;
st.top()->fr=true;
}
st.push(tmp);
}
else
{
if(!st.top()->fl)
{
st.top()->fl=true;
}
else
{
st.top()->fr=true;
}
}
while(!st.empty()&&(st.top()->fl&&st.top()->fr))
{
//cout<<st.top()->c<<endl;;
st.pop();
}
}
}
void MidSearch(BiTree &T)
{
if(!T) return ;
MidSearch(T->lch);
cout<<T->c;
MidSearch(T->rch);
}
void PreSearch(BiTree &T)
{
if(!T) return ;
cout<<T->c;
PreSearch(T->lch);
PreSearch(T->rch);
}
void PostSearch(BiTree &T)
{
if(!T) return ;
PostSearch(T->lch);
PostSearch(T->rch);
cout<<T->c;
}
int NodeCount(BiTree &T)
{
if(!T) return 0;
else return NodeCount(T->lch)+NodeCount(T->rch)+1;
}
int LeadCount(BiTree &T)
{
if(!T) return 0;
if(!T->lch&&!T->rch) return 1;
else return LeadCount(T->lch)+LeadCount(T->rch);
}
int main()
{
BiTree T;
////////////////////////////
//栈建树
int cnt=0;
char c;
while(c=getchar())
{
if(c=='\n') break;
if(c==' ') continue;
s[cnt++]=c;
}
//for(int i=0;i<cnt; i++) cout<<s[i];
CreateBiTree_Stack(T,s);
////////////////////////////
////////////////////////////
//递归建树
//CreateBiTree(T);
////////////////////////////
PreSearch(T);
puts("");
MidSearch(T);
puts("");
PostSearch(T);
puts("");
//cout<<NodeCount(T)<<endl;
//cout<<LeadCount(T)<<endl;
return 0;
}
/*
input:
A B # # C D # # #
output:
ABCD
BADC
BDCA
input:
A B C # # D E # G # # F # # #
output:
ABCDEGF
CBEGDFA
CGEFDBA
*/
递归&&栈建立二叉树+先序,后序,中序,叶子数,节点数
猜你喜欢
转载自blog.csdn.net/weixin_39132605/article/details/88926317
今日推荐
周排行