A report on training tree traversal problem solving of binary tree

Traversal of trees

Article directory

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:

4 1 6 3 5 7 2

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];//Record the post order and the middle order. 
map<int,int>R,L;//This can be replaced by a two-dimensional array. It's easy to understand with map 
//Record the nodes of the left and right subtrees, and emphasize here
//R[root] the point recorded here is the right child of the root, not the value at that point 
//[m,n] is the interval of post order, [d,f] is the interval of pre order 
int LemonCreatTree(int m,int n,int d,int f)
{
	int i;
	if(m>n)//Cross boundary judgment. It is also correct to replace d > F here, as long as one of the two sequences is judged to be out of bounds 
	{
		return 0;
	}
	int root=b[n];//Get root node 
	int p=0,num;
	for(num=d;num<=f;num++)//In the middle order, find the same point as the root and record the subscript 
	{
		if(a[num]!=root)
		{
			p++;//Record the difference between root and middle 
		}
		else
		break;
	}
	//Let's talk about the law here. The traversal mode of the post order is left and right roots, so the roots must be at the back
	//Determine the left and right subtrees in the middle order, and then p records their positions
	//The two recursions here are difficult to deal with is the post order. The middle order only needs to find the value of num, because num is the position of the middle order
	//So the right side of num is the right subtree, the boundary is set to f, the left side of num-1 is the left subtree, and the boundary is set to m 
	// For example: 1 23 4 5 6 7, p is recorded as 4, right is all points of right subtree, left is all points of left subtree
	// Then the interval of R[root] can be judged as m+p, and the right subtree and its boundary can be found from m+p-1 for L[root]
	//It's the initial point of m, because the subsequent order search is from the back to the front, so it's easy to understand  
	R[root]=LemonCreatTree(m+p,n-1,num+1,f);//Create left subtree 
	L[root]=LemonCreatTree(m,m+p-1,d,num-1);//Create right subtree 
	return root;//Return to root node 
}
void LemonBFS(int root)//Output sequence traversal 
{
	queue<int>q;
	q.push(root);
	int cnt=0;
	while(!q.empty())
	{
		int n=q.front();
		q.pop();
		printf(cnt++==0?"%d":" %d",n);//Output format problem 
		if(L[n])
		{
			q.push(L[n]);
		}
		if(R[n])
		{
			q.push(R[n]);
		}
	}
}

int main()
{
	int d;
	scanf("%d",&d);//Input node 
	for(int i=1;i<=d;i++)//Input to get post order 
	{
		scanf("%d",&b[i]);
	}
	for(int i=1;i<=d;i++)//Input to get middle order 
	{
		scanf("%d",&a[i]);
	}
	int root=LemonCreatTree(1,d,1,d);//Get left and right subtree nodes 
	LemonBFS(root);//Just add BFS and queue directly for sequence traversal 
}
Published 56 original articles, won praise and 10000 visitors+
Private letter follow

Posted by mizkit73 on Sun, 16 Feb 2020 20:32:54 -0800