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

思路:在结点的数据域中额外增加一个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. 远程桌面.【转】Win10 家庭(home)版启用远程桌面(Remote Desktop)功能

    ZC:YeJun的台式机是 Win10家庭版,默认我们想连上她的电脑是连不上的,用下面的方式,我的笔记本可以连上了 ZC:我的下载资料,存放于 "E:\BaiduYunDownload\Wi ...

  2. app下载——js设备判断

    摘自:今日头条<!doctype html> <html lang="en"> <head> <meta charset="UT ...

  3. 入门教程:.NET开源OpenID Connect 和OAuth解决方案IdentityServer v3 术语(二)

    你应该知道的在文档和对象模型中使用一些特定的术语: OpenID Connect Provider (OP) 授权服务器 Thinktecture IdentityServer v3 是一个.NET ...

  4. Spring 自定义标签配置

    前景:经常使用一些依赖于Spring的组件时,发现可以通过自定义配置Spring的标签来实现插件的注入,例如数据库源的配置,Mybatis的配置等.那么这些Spring标签是如何自定义配置的?学习Sp ...

  5. python3.7安装模块MySQLdb报错error: Microsoft Visual C++ 14.0 is required.

    error: Microsoft Visual C++ 14.0 is required. Get it with "Microsoft Visual C++ Build Tools&quo ...

  6. Selenium学习笔记(1) - 自动化测试体系与原理

    技术体系 基于代码的测试(Code-Based Testing) 基于协议的测试(Protocol-Based Testing) 基于界面的测试(GUI-Based Testing) 工作原理 基于代 ...

  7. java中的几种实体类对象(PO,VO,DAO,BO,POJO)

    一.PO :(persistant object ),持久对象 可以看成是与数据库中的表相映射的java对象.使用Hibernate来生成PO是不错的选择. 二.VO :(value object) ...

  8. nginx 相关资料

    1.https://juejin.im/post/5a2600bdf265da432b4aaaba (nginx从入门到实践) 2.https://blog.csdn.net/hzsunshine/a ...

  9. DOCKER实战案例

    操作系统:[root@yz6205 ~]# docker search busyboxNAME DESCRIPTION STARS OFFICIAL AUTOMATEDbusybox Busybox ...

  10. Agilent RF fundamentals (6) - Real TX/RX and RF model

    LNA:Low-Noise Amplifier PA: Power Amplifier1 VGA: Variable-Gain Amplifier  DC input Bias Volatage, B ...