当前位置: 代码迷 >> 综合 >> PAT练习题 P1119 Pre- and Post-order Traversals (30分)【二叉树遍历】
  详细解决方案

PAT练习题 P1119 Pre- and Post-order Traversals (30分)【二叉树遍历】

热度:59   发布时间:2024-01-26 22:54:11.0


传送门:P1119


Pre- and Post-order Traversals (30分)

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 1

Sample Output 1:

Yes
2 1 6 4 7 3 5

Sample Input 2:

4
1 2 3 4
2 4 3 1

Sample Output 2:

No
2 1 3 4



题目大意:
给你一个二叉树的前序遍历和后序遍历,让你给出中序遍历,并判断二叉树是否唯一。


解题思路:
前序遍历的规则:根节点 -> 左子树 -> 右子树
后序遍历的规则:左子树 -> 右子树 -> 根节点
我们有前序遍历可以很快知道,第一个就是根节点,那么根节点后的一个就是左子树的更节点,如果无左子树就是右子树的根节点。我们假设有左子树,那么再看后序遍历。
按后序遍历的规则我们从头开始找,一旦找到左子树的根节点,那么前面的数都是左子树上的点,由此我们可以知道左子树的长度,从而递归进行建树。
那么剩下的问题就是怎么去判断唯一性了。其实就是看除了叶子节点外的点是否都有两个子树,如果不是,那么他既可以是左子树又可以是右子树。就如样例中的前序 1 2 3 4 后序 2 4 3 1 就可以构成两种树。




AC代码:

#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<vector>
#define ll long long
#define inf 0x3f3f3f3f
using namespace std;
const int N=110;
int n;
int post[N],pre[N];
int flag;
vector<int> inorder;
struct tree
{int left,right;
}tire[N];
int build(int pre_l,int pre_r,int post_l,int post_r)
{if(pre_l>pre_r)return 0;int root=pre[pre_l];if(pre_l==pre_r)return root;int l1=pre_l+1,r2;for(r2=post_l;r2<post_r;r2++)//在后序中找左子树的根节点,确定左子树长度{if(post[r2]==pre[l1])break;}int len=r2-post_l+1;//左子树长度if(len==pre_r-pre_l)//判断是否唯一flag=1;int r1=l1+len-1;int l2=post_l;tire[root].left=build(l1,r1,l2,r2);tire[root].right=build(r1+1,pre_r,r2+1,post_r-1);return root;
}
void getinorder(int root)//得中序
{if(tire[root].left!=0)getinorder(tire[root].left);inorder.push_back(root);if(tire[root].right!=0)getinorder(tire[root].right);
}
int main()
{scanf("%d",&n);for(int i=0;i<n;i++){scanf("%d",&pre[i]);}for(int i=0;i<n;i++){scanf("%d",&post[i]);}flag=0;int root=build(0,n-1,0,n-1);getinorder(root);if(flag)printf("No\n");elseprintf("Yes\n");int len=inorder.size();for(int i=0;i<len;i++){printf("%d%c",inorder[i],i==len-1?'\n':' ');}return 0;
}
  相关解决方案