1119 Pre- and Post-order Traversals 甲级 xp_xht123
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
解题思路: 本题题意很简单,就是给定前序遍历和后序遍历需要得到中序遍历,但是dfs并不好写。
以下是dfs:
int dfs(int le , int re , int lo , int ro , string &str)
{
if(le > re) return 1;
if(per[le] != post[ro]) return 0;
int cnt = 0;
for(int i = le;i <= re;i ++) // 枚举左子树中的节点
{
string sz;//表示左子树
string sy;//表示右子树
//记录左子树中的可能方案数
int lcnt = dfs(le + 1 , i , lo , i - le - 1 + lo , sz);
//记录右子树中的可能方案数
int rcnt = dfs(i + 1 , re , ro - re + i , ro - 1 , sy);
if(lcnt && rcnt)//证明有方案
{
str = sz + to_string(per[le]) + ' ' + sy;
cnt += lcnt * rcnt;
if(cnt > 1) break;
}
}
return cnt;
}
类似于找不同的方案数的问题,分别以左子树和右子树递归,分别寻找可能的方案数,最后总方案数一定是两个方案数的乘积,并且在递归的时候分别计算出左子树和右子树在中序遍历中的顺序,分别存入字符串中,最后再将两个字符串拼接即可,最终答案一定是左子树 + 根 + 右子树
注意最终答案还有一个回车,要不然会是格式错误
完整代码:
#include<iostream>
#include<vector>
using namespace std;
const int N = 40;
int n;
int per[N] , post[N];
int dfs(int le , int re , int lo , int ro , string &str)
{
if(le > re) return 1;
if(per[le] != post[ro]) return 0;
int cnt = 0;
for(int i = le;i <= re;i ++) // 枚举左子树中的节点
{
string sz;//表示左子树
string sy;//表示右子树
//记录左子树中的可能方案数
int lcnt = dfs(le + 1 , i , lo , i - le - 1 + lo , sz);
//记录右子树中的可能方案数
int rcnt = dfs(i + 1 , re , ro - re + i , ro - 1 , sy);
if(lcnt && rcnt)//证明有方案
{
str = sz + to_string(per[le]) + ' ' + sy;
cnt += lcnt * rcnt;
if(cnt > 1) break;
}
}
return cnt;
}
int main()
{
cin>>n;
for(int i = 0;i < n;i ++) cin>>per[i];
for(int i = 0;i < n;i ++) cin>>post[i];
string str;//最终答案一定是左子树 + 根 + 右子树
int res = dfs(0 , n - 1 , 0 , n - 1 , str);
if(res > 1) puts("No");
else puts("Yes");
for(int i = 0;i < str.length() - 1;i ++) cout<<str[i];
puts("");
}
更多推荐
所有评论(0)