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

思路:在结点的数据域中额外增加一个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. note——《Tableau商业分析一点通》

    为了更好地发掘各领域的数据价值,且能对数据进行精确分析及可视化,掌握资料的脉动,做出正确的决策 人们需要一种工具:能够快速灵活地连接和整合数据,提供简单的方式实现从不同的角度去观察研究数据,计算和展示 ...

  2. python基础8 - 变量2

    1. 变量的引用 变量 和 数据 都是保存在 内存 中的 在 Python 中 函数 的 参数传递 以及 返回值 都是靠 引用 传递的 1.1 引用的概念 在 Python 中 变量 和 数据 是分开 ...

  3. redis:php-redis中有序集合 zset的使用

    ZSET(stored set) 和 set 一样是字符串的集合,不同的是每个元素都会关联一个 double 类型的 score .实现使用的是 skip list 和 hash table , sk ...

  4. Isilon

    Isilon编辑 本词条缺少信息栏,补充相关内容使词条更完整,还能快速升级,赶紧来编辑吧! 美国Isilon公司是全球群集存储系统的主要供应商,是该领域的领导者.总部位于美国华盛顿州的西雅图.创建于2 ...

  5. Myeclipse快捷键的设置以及默认的编码格式

    设置默认的编码格式

  6. Android中Application是什么?

    Application是什么? Application和Activity,Service一样,是android框架的一个系统组件,当android程序启动时系统会创建一个 application对象, ...

  7. C#实现文件与二进制互转并存入数据库

    这篇文章主要介绍了C#实现文件与二进制互转并存入数据库,本文直接给出代码实例,代码中包含详细注释,需要的朋友可以参考下 //这个方法是浏览文件对象     private void button1_C ...

  8. Activity步骤

    Android项目结构 src:Java源代码所在的文件夹 gen:自动生成的R.java所在文件夹 Android开发工具包:android.jar res:资源文件夹,包含3种 drawable: ...

  9. Python正则表达式使用过程中的小细节

    今天用Python写了个简单的爬虫程序,抓取虎扑篮球(nba.hupu.com)的首页内容,代码如下: #coding:gb2312 import urllib2, re webpage = urll ...

  10. selenium webdriver入门

    写在前面:最近在研究UI自动化测试的过程中,发现公司里通常用的是AutomanX框架,而这个框架实际上是基于selenium webdriver框架的,所以在编写测试用例时,很多语法都是直接使用sel ...