Traversal of trees
Title Description:
Given a binary tree's post order traversal and middle order traversal, please output the sequence of its sequence traversal.
It is assumed that the key values are all positive integers that are not equal to each other.
Input:
Input the first line to give a positive integer N (< 30), which is the number of nodes in the binary tree.
The second line gives the sequence of traversal. In the third line, we give the order traversal sequence. number
Separated by spaces.
Output:
Output the sequence traversal sequence of the tree in a row. Numbers are separated by 1 space,
There must be no extra spaces at the beginning and end of the line.
Sample Input:
7
2 3 1 5 7 6 4
1 2 3 4 5 6 7
Sample Output:
Binary tree traversal:
This question is about the traversal of binary trees. Before we talk about the idea of this question, let's talk about the traversal of binary trees
Four basic traversals:
1. Preorder traversal: root node - > left child point - > right child point. This sequence is characterized by its root
Always first.
2. Middle order traversal: left node - > root node - > right node. The feature of this sequence is to find
The location of the root can be divided into left and right subtrees.
3. Post order traversal: left node - > right node - > root node. This sequence is characterized by its root
Always in the last place.
4. Sequence traversal: This traversal starts from the left to the right of each layer and finishes the first layer
On the next level, his traversal can use queue structure, because it meets the characteristics of first in first out.
Thought analysis:
Train of thought analysis: this problem gives the traversal order and node number of post order and middle order. This problem can be used to
To write recursively or in a linked list, let's first talk about the idea of recursion, which is equivalent to: using DFS
To find the nodes of the left and right subtrees, we need to determine the boundary and recursion conditions as well as the position of the root
So we know the post order traversal.
1. Judge the boundary: as long as the recursion is performed, the recursion value should not exceed the interval of post order and middle order
Just fine.
2. Find the root node: because there is a post order, the root must be at the last point of the post order, according to the
This conditional recursion is OK.
3. Recursion condition: see code annotation here. It is easy to understand by combining code and annotation
Code:
#include<queue>
#include<cstdio>
#include<iostream>
#include<stack>
#include<map>
#include<set>
#include<algorithm>
#include<vector>
#include<cmath>
using namespace std;
int b[40],a[40];
map<int,int>R,L;
int LemonCreatTree(int m,int n,int d,int f)
{
int i;
if(m>n)
{
return 0;
}
int root=b[n];
int p=0,num;
for(num=d;num<=f;num++)
{
if(a[num]!=root)
{
p++;
}
else
break;
}
R[root]=LemonCreatTree(m+p,n-1,num+1,f);
L[root]=LemonCreatTree(m,m+p-1,d,num-1);
return root;
}
void LemonBFS(int root)
{
queue<int>q;
q.push(root);
int cnt=0;
while(!q.empty())
{
int n=q.front();
q.pop();
printf(cnt++==0?"%d":" %d",n);
if(L[n])
{
q.push(L[n]);
}
if(R[n])
{
q.push(R[n]);
}
}
}
int main()
{
int d;
scanf("%d",&d);
for(int i=1;i<=d;i++)
{
scanf("%d",&b[i]);
}
for(int i=1;i<=d;i++)
{
scanf("%d",&a[i]);
}
int root=LemonCreatTree(1,d,1,d);
LemonBFS(root);
}