Tree Recovery

/*已知二叉树的先序遍历序列和中序遍历序列,输出它的后序遍历序列.
    输入:DBAFCEG  FABCDEG
    输出:FACBGED
*/
#include<iostream>
#include<string>
using namespace std;
#define NULL 0
typedef char TElementType;
typedef struct BiTNode{
  TElementType data;
  struct BiTNode*lchild,*rchild;
}BiTNode,*BiTree;

void CreateTree(BiTree &T,string pre,string ino)
{
   int pos,len;
   len=pre.size();
   if(len==0) T=NULL;  //树为空
   else
   {
      pos=ino.find(pre[0]);
      if(pos==-1) T=NULL;
      else
      {
         T=new BiTNode;
         T->data=pre[0];
         if(pos==0) T->lchild=NULL;
         else
             CreateTree(T->lchild,pre.substr(1,pos),ino.substr(0,pos));
         if(pos==len-1) T->rchild=NULL;
         else
            CreateTree(T->rchild,pre.substr(pos+1),ino.substr(pos+1));
      }
   }
}//CreateTree

void print(BiTree T)  //后续遍历
{
	if(T==NULL) return;
	print(T->lchild);
	print(T->rchild);
	cout<<T->data;
}//print

int main()
{
  string s1,s2;
  BiTree T;
  cin>>s1>>s2;
  int n=s1.size();
  int ps=0,js=0;
  CreateTree(T,s1,s2);
  print(T);
  return 0;
}

  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值