Suppose(认为) that all the keys in a binary tree are distinct(不同的) positive integers. Given the postorder(后序遍历) and inorder(中序遍历) traversal(遍历) sequences(顺序), you are supposed to(你应该) output the level order(层次遍历) traversal sequence of the corresponding binary tree.
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 postorder sequence and the third line gives the inorder sequence. All the numbers in a line are separated by a space.
Output Specification:
For each test case, print in one line the level order traversal sequence of the corresponding binary tree. 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:
7
2 3 1 5 7 6 4
1 2 3 4 5 6 7
Sample Output:
4 1 6 3 5 7 2
思路:1.本人开始写这道题的时候没有任何思路,本题思路参考柳婼大神,以下仅对其思路进行描述。
2.采用递归实现从后序和中序得到层次遍历,是在递归实现从后序和中序得到前序遍历改进得来的。
3.递归实现从后序和中序得到前序遍历:根在后序遍历中最后输出,在前序遍历中最先输出,因此找到后序遍历中的根并将其最先输出即可。在每一次递归过程中仅仅是因为数据规模的不同(树和子树的关系),执行方法是一样的。具体方法为每次递归找出后序中的根(位置记为root)在中序遍历中对应的下标i,下一次先对左子树进行递归,根节点在后序遍历中的下标为root-(end-i+1),即当前根节点-(右子树的个数+1),左子树遍历完后对右子树进行遍历,根节点在后序遍历中的下标为root-1,,无论是左子树递归还是右子树递归,begin都是左(右)子树的起点,end都是左(右)子树的终点。
4.递归实现从后序和中序得到层次遍历:定义一个层次遍历位置的下标index,用map<int,int> level实现index对应index下标位置的值(map可以实现按照index大小排序)index位置的左子树位置为index*2+1,右子树位置为index*2+2,将前序遍历代码修改,即可得到层次遍历代码。
后序和中序得到前序
#include <iostream>
#include <vector>
using namespace std;
vector <int> post,in;
void pre(int root,int start,int end)
{
if(start>end)
return;
cout<<post[root]<<" ";
int i=start;
while(i<end&&post[root]!=in[i])
i++;
pre(root-(end-i+1),start,i-1);
pre(root-1,i+1,end);
}
int main()
{
int n,temp,index=0;
cin>>n;
for(int i=0;i<n*2;i++)
{
cin>>temp;
if(i<n)
post.push_back(temp);
else
in.push_back(temp);
}
pre(n-1,0,n-1);
return 0;
}
AC代码(后序和中序得到层次)
#include <iostream>
#include <map>
#include <vector>
using namespace std;
vector <int> post,in;
map<int,int> level;
void pre(int root,int start,int end,int index)
{
if(start>end)
return;
int i=start;
level[index]=post[root];
while(i<end&&post[root]!=in[i])
i++;
pre(root-(end-i+1),start,i-1,index*2+1);
pre(root-1,i+1,end,index*2+2);
}
int main()
{
int n,temp,index=0;
cin>>n;
for(int i=0;i<n*2;i++)
{
cin>>temp;
if(i<n)
post.push_back(temp);
else
in.push_back(temp);
}
pre(n-1,0,n-1,0);
map<int,int>::iterator it;
for(it=level.begin();it!=level.end();it++)
{
if(it!=level.begin())
cout<<" ";
cout<<it->second;
}
return 0;
}
2022年3月24日更新
本题还可以根据后序和中序遍历构造唯一一颗二叉树,对构造的二叉树进行层序遍历即可。
#include <iostream>
#include <vector>
#include <queue>
#include <map>
using namespace std;
struct tree {
int data;
tree* lchild;
tree* rchild;
};
vector<int> post, in, ans;
map<int, int> index;
void createTree(int data, tree* &root) {
if (root == NULL) {
root = new tree;
root->data = data;
root->lchild = NULL;
root->rchild = NULL;
return;
}
if (index[root->data] > index[data]) createTree(data, root->lchild);
if(index[root->data] <= index[data]) createTree(data,root->rchild);
}
void levelorder(tree* root) {
queue<tree*> q;
q.push(root);
while (!q.empty()) {
ans.push_back((q.front())->data);
if ((q.front())->lchild != NULL) q.push((q.front())->lchild);
if ((q.front())->rchild != NULL) q.push((q.front())->rchild);
q.pop();
}
}
int main() {
int N;
cin >> N;
post.resize(N);
in.resize(N);
for (int i = 0; i < N; i++) cin >> post[i];
for(int i = 0; i < N; i++) {
cin >> in[i];
index[in[i]] = i;
}
tree* root = NULL;
for (int i = N - 1; i >= 0; i--) createTree(post[i], root);
levelorder(root);
for (int i = 0; i < ans.size(); i++) {
if (i != 0) cout << " ";
cout << ans[i];
}
return 0;
}
更多PAT甲级题目:请访问PAT甲级刷题之路--题目索引+知识点分析(正在进行),感谢支持!