题目大意:
给定二叉树的中序和后序遍历,输出“层次”遍历,这个层次遍历从根节点的子节点开始,先从左到右输出,然后从右到左输出。。。循环往复。
解题思路:
这个题我写的复杂了一点,关于输出哪里我用了两个双端队列模拟了输出的扩展情况,即从左到右和从右到左,所以代码较难理解。维护一个度参数根据奇偶度判断是从右到左还是从左到右输出会方便很多。
代码如下:
#include<iostream>
#include<vector>
#include<queue>
#include<deque>
#include<cstdio>
using namespace std;
int n,post[40],in[40];
struct node
{
int data;
node* lchild;
node* rchild;
};
node* create(int postl,int postr,int inl,int inr)//建树
{
if(postr<postl)return NULL;
node* root=new node;
root->data=post[postr];
int k