标签:uva
Tree Recovery
Little Valentine liked playing with binary trees very much. Her favorite game was constructing randomly looking binary trees with capital letters in the nodes.
This is an example of one of her creations:
D / / B E / \ / \ \ A C G / / F
To record her trees for future generations, she wrote down two strings for each tree: a preorder traversal (root, left subtree, right subtree) and an inorder traversal (left subtree, root, right subtree).
For the tree drawn above the preorder traversal is DBACEGF and the inorder traversal is ABCDEFG.
She thought that such a pair of strings would give enough information to reconstruct the tree later (but she never tried it).
Now, years later, looking again at the strings, she realized that reconstructing the trees was indeed possible, but only because she never had used the same letter twice in the same tree.
However, doing the reconstruction by hand, soon turned out to be tedious.
So now she asks you to write a program that does the job for her!
Input is terminated by end of file.
DBACEGF ABCDEFG BCAD CBAD
ACBFGED CDAB
题目大意:
给你一棵二叉树的前序和中序,求后序。
解题思路:
递归呀。
代码一:
#include<iostream> #include<string> #include<cstdio> using namespace std; const int maxN = 30; struct Node{ char ch; int l,r; Node(int l0=0,int r0=0){l=l0,r=r0;} }tree[maxN]; string pre,in,post; int index; void build(string preOrder,string inOrder){ int temp=index; tree[temp]=Node(-1,-1); tree[temp].ch=preOrder[0]; //if(preOrder.length()==1) return ; for(int i=0;i<preOrder.length();i++){ if(inOrder[i]==preOrder[0]){ if(i>0){//如果有lson string lpre=preOrder.substr(1,i); string lin=inOrder.substr(0,i); tree[temp].l=++index; build(lpre,lin); } if(i<preOrder.length()-1){//如果有rson string rpre=preOrder.substr(i+1); string rin=inOrder.substr(i+1); tree[temp].r=++index; build(rpre,rin); } } } } void solve(){ post.clear(); index=0; build(pre,in); } void postMake(int cnt){ if(tree[cnt].l!=-1) postMake(tree[cnt].l); if(tree[cnt].r!=-1) postMake(tree[cnt].r); post.push_back(tree[cnt].ch); } void output(){ cout<<post<<endl; } int main(){ while(cin >> pre >> in){ solve(); postMake(0); output(); } return 0; }
/* UVa536 Tree Recovery Coded by Guojin Zhu Run Time 0.008s AC on November 2,2012 */ #include<iostream> #include<string> using namespace std; /////////////////// class TreeRecovery{ private: string preord; string inord; string postorder; void reconstruct(string p, string i); public: void initial(string p, string i){ preord = p; inord = i; postorder.clear(); } void computing(){reconstruct(preord, inord);} void outResult(){cout << postorder << endl;} }; void TreeRecovery::reconstruct(string preord, string inord){ int sz = preord.size(); if(sz > 0){ int p = int(inord.find(preord[0])); reconstruct(preord.substr(1, p), inord.substr(0, p)); //left subtree reconstruct(preord.substr(p+1, sz-p-1), inord.substr(p+1, sz-p-1)); //right subtree postorder.push_back(preord[0]); //root } } ///////////////////// int main(){ TreeRecovery tr; string p, i; while(cin >> p >> i){ tr.initial(p, i); tr.computing(); tr.outResult(); } return 0; } /* INPUT DBACEGF ABCDEFG BCAD CBAD AB BA -------------- OUTPUT ACBFGED CDAB BA */
UVA 536 Tree Recovery(由前序中序求后序,经典)
标签:uva
原文地址:http://blog.csdn.net/hush_lei/article/details/40680157