层序遍历
设二叉树的根节点所在层数为1,层序遍历就是从所在二叉树的根节点出发,首先访问第一层的树根节点,然后从左到右访问第2层. 上的节点,接着是第三层的节点,以此类推,自上而下,自左至右逐层访问树的结点的过程就是层序遍历
例题: 给出一颗二叉树的后序遍历序列和中序遍历序列,求这颗二叉树的层序遍历序列。
(输入样例和输出样例如运行结果所示)
#include<cstdio>
#include<cstring>
#include<queue>
#include<algorithm>
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