Tree Recovery
Time Limit: 1000MS | Memory Limit: 65536K |
Total Submissions: 15363 | Accepted: 9510 |
Description
Little Valentineliked playing with binary trees very much. Her favorite game was constructingrandomly 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 eachtree: a preorder traversal (root, left subtree, right subtree) and an inordertraversal (left subtree, root, right subtree). For the tree drawn above thepreorder traversal is DBACEGF and the inorder traversal is ABCDEFG.
She thought that such a pair of strings would give enough information toreconstruct the tree later (but she never tried it).
Now, years later, looking again at the strings, she realized thatreconstructing the trees was indeed possible, but only because she never hadused 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
The input will contain one or more testcases.
Each test case consists of one line containing two strings preord and inord,representing the preorder traversal and inorder traversal of a binary tree.Both strings consist of unique capital letters. (Thus they are not longer than26 characters.)
Input is terminated by end of file.
Output
For each testcase, recover Valentine's binary tree and print one line containing the tree'spostorder traversal (left subtree, right subtree, root).
Sample Input
DBACEGF ABCDEFG
BCAD CBAD
Sample Output
ACBFGED
CDAB
思路:先建树在后序遍历:
建树
代码:
class TreeNode{
public:
char val;
TreeNode*left,*right;
TreeNode(char val){this->val= val;}
static TreeNode*buildTree(vector<char>&preorder, vector<char>&inorder){
map<char,int> map;
for(int i=0; i<inorder.size(); i++)
map[inorder[i]]= i;
return helper(0,0, inorder.size()-1, preorder, inorder,map);
}
static TreeNode*helper(int preStart,int inStart,int inEnd,
vector<char>&preorder, vector<char>&inorder, map<char,int>&map){
if(preStart>= preorder.size()|| inStart>inEnd)
returnNULL;
TreeNode*root=new TreeNode(preorder[preStart]);
int inIndex= map[preorder[preStart]];
root->left= helper(preStart+1, inStart, inIndex-1,
preorder, inorder, map);
root->right= helper(preStart+ inIndex- inStart+1,
inIndex+1, inEnd, preorder, inorder, map);
return root;
}
}
后序遍历代码:
static vector<char> postOrderTravel(TreeNode*root){
vector<char>*v=new vector<char>;
postOrderTravel(root,*v);
return*v;
}
staticvoid postOrderTravel(TreeNode*root, vector<char>&v){
if(root==NULL)return;
if(root->left!=NULL) postOrderTravel(root->left, v);
if(root->right!=NULL) postOrderTravel(root->right, v);
v.push_back(root->val);
}
vector<char> postOrderTravel(){
return postOrderTravel(this);
}
完整代码:
//POJ 2255 Tree Recovery
#include <vector>
#include <string>
#include <iostream>
#include <map>
usingnamespace std;
class TreeNode{
public:
char val;
TreeNode *left,*right;
TreeNode(char val){ this->val = val;}
static TreeNode*buildTree(vector<char>&preorder, vector<char>&inorder){
map<char,int> map;
for(int i= 0; i<inorder.size(); i++)
map[inorder[i]]= i;
return helper(0,0, inorder.size()- 1, preorder, inorder,map);
}
static TreeNode*helper(int preStart,int inStart,int inEnd,
vector<char>&preorder, vector<char>&inorder, map<char,int>&map){
if(preStart >= preorder.size()|| inStart>inEnd)
returnNULL;
TreeNode *root= new TreeNode(preorder[preStart]);
int inIndex= map[preorder[preStart]];
root->left= helper(preStart+ 1, inStart, inIndex- 1,
preorder, inorder, map);
root->right= helper(preStart+ inIndex - inStart+ 1,
inIndex+1, inEnd, preorder, inorder, map);
return root;
}
static vector<char> postOrderTravel(TreeNode*root){
vector<char>*v =new vector<char>;
postOrderTravel(root,*v);
return*v;
}
staticvoid postOrderTravel(TreeNode*root, vector<char>&v){
if(root ==NULL)return;
if(root->left!= NULL) postOrderTravel(root->left, v);
if(root->right!= NULL) postOrderTravel(root->right, v);
v.push_back(root->val);
}
vector<char> postOrderTravel(){
return postOrderTravel(this);
}
};
int main(int argc,char const *argv[])
{
//freopen("in.txt","r", stdin);
string str;
while(cin >> str){
vector<char> preorder(str.begin(), str.end());
cin >> str;
vector<char> inorder(str.begin(), str.end());
TreeNode *root= TreeNode::buildTree(preorder, inorder);
vector<char> ans= root->postOrderTravel();
for(int i= 0; i<ans.size(); i++) cout << ans[i];
cout << endl;
}
return0;
}