文章

9

粉丝

126

获赞

8

访问

19.9k

头像
差别在建树方法

#include <bits/stdc++.h>

using namespace std;

typedef struct node {
	char data;
	struct node *lchild,*rchild;
} node,*Tree;

void createTree (Tree &T,string pre,string in) {
	T=new node;T->lchild=NULL;T->rchild=NULL;
	T->data=pre[0];
	if(pre.size()==1) return;
	
	int mid=in.find(pre[0]);
	if(mid!=0){
		createTree (T->lchild,pre.substr(1,mid+1),in.substr(0,mid));
	}
	if(mid!=in.size()-1){
		createTree (T->rchild,pre.substr(mid+1),in.substr(mid+1));
	}
}

void postorder (Tree T) {
	if(T!=NULL){
		postorder(T->lchild);
		postorder(T->rchild);
		cout<<T->data;
	}
	
}

int main() {
	string pre,in;
	while(cin>>pre>>in){
		Tree T=NULL;
		createTree(T,pre,in);
		postorder(T);cout<<endl;
	}
}

 

登录查看完整内容


登录后发布评论

暂无评论,来抢沙发