Suppose that all the keys in a binary tree are distinct positive integers. A unique binary tree can be determined by a given pair of postorder and inorder traversal sequences, or preorder and inorder traversal sequences. However, if only the postorder and preorder traversal sequences are given, the corresponding tree may no longer be unique.
Now given a pair of postorder and preorder traversal sequences, you are supposed to output the corresponding inorder traversal sequence of the tree. If the tree is not unique, simply output any one of them.
Input Specification:
Each input file contains one test case. For each case, the first line gives a positive integer N (<=30), the total number of nodes in the binary tree. The second line gives the preorder sequence and the third line gives the postorder sequence. All the numbers in a line are separated by a space.
Output Specification:
For each test case, first printf in a line "Yes" if the tree is unique, or "No" if not. Then print in the next line the inorder traversal sequence of the corresponding binary tree. If the solution is not unique, any answer would do. It is guaranteed that at least one solution exists. All the numbers in a line must be separated by exactly one space, and there must be no extra space at the end of the line.
Sample Input 1:7 1 2 3 4 6 7 5 2 6 7 4 5 3 1Sample Output 1:
Yes 2 1 6 4 7 3 5Sample Input 2:
4 1 2 3 4 2 4 3 1Sample Output 2:
No 2 1 3 4
(先说一句,题目还不错,很值得动手思考并且去实现。)
题意:根据前序遍历和后序遍历建树,输出中序遍历序列,序列可能不唯一,输出其中一个即可。
已知前序遍历和后序遍历序列,是无法确定一棵二叉树的,原因在于如果只有一棵子树可能是左孩子也有可能是右孩子。由于只要输出其中一个方案,所以假定为左孩子即可。下面就是如何根据前序和后序划分出根节点和左右孩子,这里需要定义前序和后序的区间范围,分别为[preL,preR],[postL,postR]。
一开始区间都为[1,n],可以发现前序的第一个和后序的最后一个为根节点root,前序的第二个值val为其某子树的根节点(但还无法确定是左孩子or右孩子)。在后序中找对应的值所在的位置postIdx,则postIdx之前的节点均为val的孩子节点,统计其个数num。那么我们就可以划分区间:
若num个数=preR-preL-1,即val后面的个数都是其子节点,那么二叉树不唯一,将其作为root的左子树处理。
否则划分为左子树区间和右子树对应的前序和后序区间,顺便更新下root的左孩子preL+1,右孩子preL+num+2:
preOrder:[preL+1,preL+num+1],postOrder:[postL,postIdx];
preOrder:[preL+num+2,preR],postOrder:[postIdx+1,postR-1];
然后递归划分即可
拿样例举例:
1 (2) [3 {4 6 7} <5>]
(2) [{6 7 4} <5> 3] 1
不同的括号对应不同的子树区间
第一次递归划分了(2)-(2),[3 4 6 7 5]-[6 7 4 5 3]
由于(2)只有一棵,不继续划分。
第二次递归划分了{4 6 7}-{6 7 4},<5>-<5>
第三次递归划分了(6)-(6),(7)-(7)
结束
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
const int N =33;
struct node
{
int l;
int r;
} tree[N];
int pre[N],pos[N],flag,a[N],k;
int build(int l1,int r1,int l2,int r2)
{
int i;
if(l1>r1) return-1;
tree[pre[l1]].l=-1;
tree[pre[l1]].r=-1;
if(l1==r1) {
a[k++]=pre[l1];
return pre[l1];
}
int rt=pre[l1];
// tree[rt].l=pre[l1+1];
for( i=l2;i<=r2;i++) {
if(pos[i]==pre[l1+1]) break;
}
int num=i-l2;
tree[rt].l=build(l1+1,l1+1+num,l2,l2+num);
a[k++]=rt;
tree[rt].r=build(l1+2+num,r1,l2+1+num,r2);
if(tree[rt].l*tree[rt].r<0) flag=0;
return rt;
}
int main()
{
int n,i,j,root;
cin>>n;
for(i=1;i<=n;i++) cin>>pre[i];
for(i=1;i<=n;i++) cin>>pos[i];
flag=1,k=0;
root=build(1,n,1,n);
if(flag) cout<<"Yes"<<endl;
else cout<<"No"<<endl;
for(i=0;i<k;i++) {
if(i) cout<<" ";
cout<<a[i];
}
cout<<endl;
return 0;
}