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

思路:在结点的数据域中额外增加一个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. MySQL无法启动几种常见问题小结

    问题1:目录.文件权限设置不正确 MySQL的$datadir目录,及其下属目录.文件权限属性设置不正确,导致MySQL无法正常读写文件,无法启动. 错误信息例如: 复制代码 代码如下:[code] ...

  2. LeetCode第[44]题(Java):Wildcard Matching

    题目:通配符匹配 难度:hard 题目内容: Given an input string (s) and a pattern (p), implement wildcard pattern match ...

  3. python学习笔记(接口自动化框架 V1.0)

    之前是利用python自带的unittest测试框架 这次自己设计一个 之后再一点点往里面加功能 (ps:当然这个框架真的是很简单..很简单...很简单...) excel文件格式: #!/usr/b ...

  4. MySQL忘记密码解决方案

    1.修改本地mysql目录中的my.ini文件 添加skip-grant-tables 2.在win +r 输入cmd,进行mysql的重启启动操作 net stop MySQL 停止服务      ...

  5. 51nod-1259-分块+dp

    http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1259 1259 整数划分 V2 基准时间限制:1 秒 空间限制:1310 ...

  6. vue.js 1.0中用v-for遍历出的li中的@click事件在移动端无效

    在vue.js使用v-for遍历出的li中的@click事件在移动端无效,在网页端可以执行,代码如下 <template> <div class="rating-secti ...

  7. 初识Linux(五)--VI/VIM编辑器

    我们操作文件,终究离不开编辑文件,对文件内容的编辑,Linux系统下,我们通常使用VI/VIM来编辑文件.VI是每个Linux都会自带的文本编辑器,VIM是VI的增强版,可能有些发行版本没有自带,可以 ...

  8. node 一站式 学习 教程

    还是比较全面的, 包括了 : monogoDB的安装 使用 , 各种插件, 中间件的介绍, 路由的介绍, 各种数据库框架的介绍, 测试介绍;  掌握后应该可以开发一个中型的程序, 大型程序因为有性能的 ...

  9. Webpack 热部署检测不到文件变化问题

    Webpack 热部署检测不到文件变化问题 今天在用Webpack开发的时候,突然发现文件变动后热部署功能不工作了,感觉好像是webpack检测不到文件的修改了.折腾了半天,开始一直以为是自己的代码有 ...

  10. 9.链表中倒数第k个结点[FindReverseKthLinkedListNode]

    [题目] 输入一个单向链表,输出该链表中倒数第k个结点.链表的倒数第0个结点为链表的尾指针.链表结点定义如下:  C++ Code  12345   struct ListNode {     int ...