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
#include <iostream>
#include<cstring>
#include<algorithm>
#include<queue>
using namespace std;
const int maxn = 50;
struct node
{
int data;
node* lchild;
node* rchild;
};
int pre[maxn], in[maxn], post[maxn];//先序、中序、后序
int n;//结点的个数
//当前二叉树的后序序列区间为[postL,postR],中序序列区间为[inL,inR]
//create函数返回构建出的二叉树的根结点地址
node* create(int postL, int postR, int inL, int inR)
{
if (postL > postR)
{
return NULL;//后序序列长度小于等于0时,直接返回
}
node* root = new node;
root->data = post[postR];//新节点的数据域为根节点的值
int k;
for (k = inL; k <= inR; k++)
{
if (in[k] == post[postR])//在中序序列中找到in[k]==pre[L]的点
{
break;
}
}
int numLeft = k - inL;//左子树结点的个数
root->lchild = create(postL, postL + numLeft - 1, inL, k - 1);
root->rchild = create(postL + numLeft, postR - 1, k + 1, inR);
return root;
}
int num = 0;//已输出结点的个数
void BFS(node* root)
{
queue<node*>q;//队列里面存放的是地址
q.push(root);//将根结点的地址入队
while (!q.empty())
{
node* now = q.front();//取出队首元素
q.pop();
printf("%d", now->data);//访问队首元素
num++;
if (num < n) printf(" ");
if (now->lchild != NULL)q.push(now->lchild);//左子树非空
if (now->rchild != NULL)q.push(now->rchild);//右子树非空
}
}
int main()
{
scanf("%d", &n);
for (int i = 0; i < n; i++)
{
scanf("%d", &post[i]);
}
for (int i = 0; i < n; i++) {
scanf("%d", &in[i]);
}
node* root = create(0, n - 1, 0, n - 1);//建树
BFS(root);//层次遍历
return 0;
}
复习:
给定一个二叉树的中序遍历序列和其他一种形式的遍历序列(常见的是前序和后序序列)可以重建一棵二叉树,这道题就是给定后序序列和中序序列重建二叉树后再用BFS的方法进行层次遍历。重建二叉树的思路,如下图,后序序列给出根节点的信息,中序序列按照给出根节点左右子树的信息。
二刷代码:
#include <iostream>
#include<queue>
using namespace std;
const int N = 31;
struct node
{
int value;
node* l_child;
node* r_child;
};
int post_list[31];
int in_List[31];
int pre_List[31];
node* creatBinaryTree(int postL, int postR, int inL, int inR)
{
if (postL > postR || inL > inR)
return nullptr;
node* root = new node;
root->value = post_list[postR];
int k;
for (int i = inL; i <= inR; i++)
{
if (in_List[i] == post_list[postR])
{
k = i;
break;
}
}
int numLeft = k - inL;//左子树节点的个数
root->l_child = creatBinaryTree(postL, postL +numLeft-1, inL, k - 1);
root->r_child = creatBinaryTree(postL + numLeft, postR - 1, k + 1, inR);
return root;
}
void layerTravel(node*root,int n)
{
if (root == nullptr)
return;
int countNum = 0;
queue<node*>q;
q.push(root);
while (!q.empty())
{
++countNum;
node* temp = q.front();
q.pop();
printf("%d", temp->value);
if (countNum < n) printf(" ");
if (temp->l_child)
q.push(temp->l_child);
if (temp->r_child)
q.push(temp->r_child);
}
}
int main()
{
int n,num;
scanf("%d", &n);
for (int i = 0; i < n; i++)
{
scanf("%d", &num);
post_list[i] = num;
}
for (int i = 0; i < n; i++)
{
scanf("%d", &num);
in_List[i] = num;
}
node* root=creatBinaryTree(0, n - 1, 0, n - 1);
layerTravel(root, n);
return 0;
}