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:
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!
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!
The input will contain one or more test cases.
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 than 26 characters.)
Input is terminated by end of file.
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 than 26 characters.)
Input is terminated by end of file.
For each test case, recover Valentine's binary tree and print one line containing the tree's postorder traversal (left subtree, right subtree, root).
DBACEGF ABCDEFG BCAD CBAD
ACBFGED CDAB
题意:一个二叉树,给你中序前序遍历,还原树结构。
题解:前序数组 A【i~j】, 中序数组 B【n~m】
假设这两个数组对应一个二叉树的遍历结果。
根据前序性质,可知第一个值 A【i】 就是 树的根节点root,
然后再根据中序性质,找到 root 对应在中序数组 B【n~m】 中的位置为 K (B【K】==root) ,则 K左边的区间 B【n~k-1】都是root 的左子树节点的中序遍历,K右边的值B【k+1,m】都是root的右子树节点的中序遍历,左子树和右子树的前序遍历对应在A数组中的位置也可以找到。既然找到 左子树和右子树的前序和中序 在A数组和B数组的下标区间 ,则可以继续确定左子树的根节点和左子树和右子树,同理。。。
总结来说,就是前序确定根节点,中序确定根节点的左子树和右子树。
代码1:
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <iostream>
#define mem(a,c) memset(a,c,sizeof(a));
using namespace std;
char a[5000],b[5000];
char tree[5000<<2];
void build(int l,int r,int ll,int rr,int rt){//l,r就是A数组的下标区间,ll,rr是对应B[l~r]的B数组的下标区间。
if(l>r)return;
tree[rt]=a[l];
if(l==r)return;
int cur;
for(int i=ll;i<=rr;i++){
if(b[i]==a[l]){
cur=i;break;//确定了根节点root=a【l】=b【cur】 的位置cur,然后把左子树和右子树分开,这时b【ll~ cur-1】即左子树,b【cur+1 ~ rr】即右子树
}
}
int le=cur-ll;
build(l+1,l+le,ll,cur-1,rt<<1);//需要找到左子树的前序在A数组的对应。
build(l+le+1,r,cur+1,rr,rt<<1|1);
}
void dfs(int rt){
if(!tree[rt]){
return;
}
dfs(rt<<1);
dfs(rt<<1|1);
cout<<tree[rt];
}
int main()
{
while(cin>>a>>b){
mem(tree,0);
int len=strlen(a);
int rt=1;
build(0,len-1,0,len-1,1);
dfs(1);
cout<<endl;
}
return 0;
}