给出一棵满二叉树的先序遍历,有两种节点:字母节点(A-Z,无重复)和空节点(#)。要求这个树的中序遍历。输出中序遍历时不需要输出#。满二叉树的层数n满足1<=n<=5。
Sample Input:
ABC#D#E
Sample Output:
CBADE
#include<iostream>
#include<string>
using namespace std;
string str;
struct node{
char data;
node *lchild,*rchild;
};
node* build(node *root,int l,int r){
if(r>=l){
int length=(r-l)/2;
root=new node;
root->data=str[l];
root->lchild=build(root->lchild,l+1,l+length);
root->rchild=build(root->rchild,r-length+1,r);
return root;
}
else
return NULL;
}
void inOrder(node *p){
if(p->lchild!=NULL)
inOrder(p->lchild);
if(p->data!='#' )
cout<<p->data;
if(p->rchild!=NULL)
inOrder(p->rchild);
return;
}
void deleteNode(node* root){
if(root==NULL)
return;
if(root->lchild!=NULL)
free(root->lchild);
if(root->rchild!=NULL)
free(root->rchild);
free(root);
}
int main(){
node *p;
cin>>str;
p=build(p,0,str.length()-1);
inOrder(p);
deleteNode(p);
return 0;
}