题意:中序序列+后序序列构建二叉树,之字形输出其层序序列。

思路:在结点的数据域中额外增加一个layer表示结点所在的层次,并定义vector<int> zigzag[maxn]存放最终结果。按照常规顺序进行层序遍历,将第i层的值存入到zigzag[i]中,最后输出时,第偶数层从左向右输出,第奇数层反之。

代码:

#include <cstdio>
#include <queue>
#include <vector>
using namespace std;
;
int in[maxn],post[maxn];
vector<int> zigzag[maxn];
;

struct Node{
    int val;
    int layer;
    Node *lchild,*rchild;
    Node(),lchild(NULL),rchild(NULL){}
};

Node* buildBiTree(int pL,int pR,int inL,int inR)
{
    if(pL>pR) return NULL;
    int rootval=post[pR];
    Node* root=new Node(rootval);
    int pos=inL;
    while(in[pos]!=rootval) pos++;
    int leftCnt=pos-inL;
    root->lchild=buildBiTree(pL,pL+leftCnt-,inL,pos-);
    root->rchild=buildBiTree(pL+leftCnt,pR-,pos+,inR);
    return root;
}

void levelOrderTraversal(Node* root)
{
    queue<Node*> q;
    root->layer=;
    q.push(root);
    while(!q.empty()){
        Node* pNode=q.front();
        q.pop();
        if(pNode->layer > maxLayer) maxLayer=pNode->layer;
        zigzag[pNode->layer].push_back(pNode->val);
        if(pNode->lchild){
            pNode->lchild->layer=pNode->layer+;
            q.push(pNode->lchild);
        }
        if(pNode->rchild){
            pNode->rchild->layer=pNode->layer+;
            q.push(pNode->rchild);
        }
    }
}

int main()
{
    //freopen("pat.txt","r",stdin);
    int n;
    scanf("%d",&n);
    ;i<n;i++) scanf("%d",&in[i]);
    ;i<n;i++) scanf("%d",&post[i]);
    Node* root=buildBiTree(,n-,,n-);
    levelOrderTraversal(root);
    printf("%d",root->val);
    ;i<=maxLayer;i++){
        ==){
            ;j<zigzag[i].size();j++)
                printf(" %d",zigzag[i][j]);
        }else{
            ;j>=;j--)
                printf(" %d",zigzag[i][j]);
        }
    }
    ;
}

1127 ZigZagging on a Tree的更多相关文章

  1. 1127 ZigZagging on a Tree (30 分)

    1127 ZigZagging on a Tree (30 分) Suppose that all the keys in a binary tree are distinct positive in ...

  2. PAT甲级 1127. ZigZagging on a Tree (30)

    1127. ZigZagging on a Tree (30) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue ...

  3. PAT甲级1127. ZigZagging on a Tree

    PAT甲级1127. ZigZagging on a Tree 题意: 假设二叉树中的所有键都是不同的正整数.一个唯一的二叉树可以通过给定的一对后序和顺序遍历序列来确定.这是一个简单的标准程序,可以按 ...

  4. PAT 1127 ZigZagging on a Tree[难]

    1127 ZigZagging on a Tree (30 分) Suppose that all the keys in a binary tree are distinct positive in ...

  5. pat 甲级 1127. ZigZagging on a Tree (30)

    1127. ZigZagging on a Tree (30) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue ...

  6. PAT 甲级 1127 ZigZagging on a Tree

    https://pintia.cn/problem-sets/994805342720868352/problems/994805349394006016 Suppose that all the k ...

  7. PAT 1127 ZigZagging on a Tree

    Suppose that all the keys in a binary tree are distinct positive integers. A unique binary tree can ...

  8. PAT Advanced 1127 ZigZagging on a Tree (30) [中序后序建树,层序遍历]

    题目 Suppose that all the keys in a binary tree are distinct positive integers. A unique binary tree c ...

  9. PAT甲题题解-1127. ZigZagging on a Tree (30)-中序、后序建树

    根据中序遍历和前序遍历确定一棵二叉树,然后按“层次遍历”序列输出.输出规则:除根节点外,接下来每层的节点输出顺序是:先从左到右,再从右到左,交替输出 #include <iostream> ...

随机推荐

  1. TCP_AIO_Server_ZC_01

    ZC: 这个例子是,1个skt 投递 1个未决的接收操作 1.package aio.Client; 1.1.这是一个 测试的客户端的代码: package aio.Client; import ja ...

  2. javascript 对象简单介绍(二)

    JavaScript Array(数组) 对象数组对象的作用是:使用单独的变量名来存储一系列的值. 什么是数组?数组对象是使用单独的变量名来存储一系列的值.如果你有一组数据(例如:车名字),存在单独变 ...

  3. gulp 相关文章

    1.https://www.cnblogs.com/sxz2008/p/6370221.html 2.https://www.cnblogs.com/wujie520303/p/4964931.htm ...

  4. 探究platform_driver中“多态”思想

    问题最初是下面的两段代码引出的: static struct platform_driver sonypi_driver = { .driver = { .name = "sonypi&qu ...

  5. 简短的perl程序

    简短的perl程序能够实现大功能.    perl是如何做到的呢?  1. 默认变量     如果没有向函数提供参数值,则默认参数为$_:     如果没有变量用于接收一个表达式的值,则默认接收变量为 ...

  6. 5天不再惧怕多线程——第一天 尝试Thread

    随笔 - 218  文章 - 1  评论 - 3819 5天不再惧怕多线程——第一天 尝试Thread   原本准备在mongodb之后写一个lucene.net系列,不过这几天用到多线程时才发现自己 ...

  7. Unity3D事件顺序与功能

    Unity3D中所有控制脚本的基类MonoBehaviour有一些虚函数用于绘制中事件的回调,也可以直接理解为事件函数,例如大家都很清楚的Start,Update等函数,以下做个总结. Awake 当 ...

  8. Java(Android)线程池妙用

    介绍new Thread的弊端及Java四种线程池的使用,对Android同样适用.本文是基础篇,后面会分享下线程池一些高级功能. 1.new Thread的弊端执行一个异步任务你还只是如下new T ...

  9. 【设计模式】calendar的单例需求和实现

    calendar单例需求: 参数:有default的calendar file 1.如果无实例,无参数调用,取default,检查是否合法,存入实例 2.如果无实例,有参数调用,检查是否合法,存入实例 ...

  10. 日常生活小技巧 -- 惠普 Windows10 进入安全模式

    今天手贱,是真的很贱.将用户模式从管理员组改为标准用户 方法是:WIN+R 打开 control userpasswords2 然后出现了用户账户控制,你要允许此应用对你的设备进行更改吗?最关键的是没 ...