题目链接:点击打开链接
Binary Tree Traversals
Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 9346 Accepted Submission(s): 4222
Problem Description
A binary tree is a finite set of vertices that is either empty or consists of a root r and two disjoint binary trees called the left and right subtrees. There are three most important ways in which the vertices of a binary tree can be systematically traversed or ordered. They are preorder, inorder and postorder. Let T be a binary tree with root r and subtrees T1,T2.
In a preorder traversal of the vertices of T, we visit the root r followed by visiting the vertices of T1 in preorder, then the vertices of T2 in preorder.
In an inorder traversal of the vertices of T, we visit the vertices of T1 in inorder, then the root r, followed by the vertices of T2 in inorder.
In a postorder traversal of the vertices of T, we visit the vertices of T1 in postorder, then the vertices of T2 in postorder and finally we visit r.
Now you are given the preorder sequence and inorder sequence of a certain binary tree. Try to find out its postorder sequence.
In a preorder traversal of the vertices of T, we visit the root r followed by visiting the vertices of T1 in preorder, then the vertices of T2 in preorder.
In an inorder traversal of the vertices of T, we visit the vertices of T1 in inorder, then the root r, followed by the vertices of T2 in inorder.
In a postorder traversal of the vertices of T, we visit the vertices of T1 in postorder, then the vertices of T2 in postorder and finally we visit r.
Now you are given the preorder sequence and inorder sequence of a certain binary tree. Try to find out its postorder sequence.
![](https://i-blog.csdnimg.cn/blog_migrate/13d014c9e5e3240eee00ab3d73dc79e8.jpeg)
Input
The input contains several test cases. The first line of each test case contains a single integer n (1<=n<=1000), the number of vertices of the binary tree. Followed by two lines, respectively indicating the preorder sequence and inorder sequence. You can assume they are always correspond to a exclusive binary tree.
Output
For each test case print a single line specifying the corresponding postorder sequence.
Sample Input
9 1 2 4 7 3 5 8 9 6 4 7 2 1 8 5 9 3 6
Sample Output
7 4 2 8 9 5 6 3 1
Source
题意:题意没啥好说的,就是给你一颗树的先序遍历和中序遍历求后序遍历。
题解:先序遍历的第一个数就是树的根,然后在中序遍历找到这个数,那么在中序遍历中这个数的左边就是根的左子树,右边就是这个根的右子树,然后递归的在继续找,把结果保存下来就行。还是比较简单的一道题。
这个题是开学补牛客网假期培训班的视频看到的,下面是链接,他讲到这个题了,但是他给的代码有bug,不过思路是正确的。大家可以看看他讲的,讲的还是不错的。后来我把他的代码改了改,过了这道题。
视频链接:点击打开链接
AC代码:
#include <bits/stdc++.h>
using namespace std;
int n, tot;
int pre[10001],in[10001];
vector <int> post;
void xx(int l, int r){
if(l > r) return;
tot++;
int root=pre[tot];
int m=0;
for(int i = l; i <= r; i++)
if(in[i] == pre[tot])
{
m=i;
break;
}
xx(l,m-1); xx(m+1,r);
post.push_back(root);
}
int main()
{
while(scanf("%d", &n) == 1)
{
post.clear();
for(int i = 1; i <= n; i++)
scanf("%d", &pre[i]);
for(int i = 1; i <= n; i++)
scanf("%d", &in[i]);
tot = 0;
xx(1, n);
cout << post[0];
for(int i=1;i<n;i++) cout << " " << post[i];
cout << endl;
}
}