1127 ZigZagging on a Tree

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

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

代码:

#include <cstdio>
#include <queue>
#include <vector>
using namespace std;
const int maxn=35;
int in[maxn],post[maxn];
vector<int> zigzag[maxn];
int maxLayer=0;

struct Node{
    int val;
    int layer;
    Node *lchild,*rchild;
    Node(int v):val(v),layer(1),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-1,inL,pos-1);
    root->rchild=buildBiTree(pL+leftCnt,pR-1,pos+1,inR);
    return root;
}

void levelOrderTraversal(Node* root)
{
    queue<Node*> q;
    root->layer=1;
    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+1;
            q.push(pNode->lchild);
        }
        if(pNode->rchild){
            pNode->rchild->layer=pNode->layer+1;
            q.push(pNode->rchild);
        }
    }
}

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

原文地址:https://www.cnblogs.com/kkmjy/p/9574417.html

时间: 2024-10-08 13:46:56

1127 ZigZagging on a Tree的相关文章

1127 ZigZagging on a Tree (30 分)树的层次遍历

1127 ZigZagging on a Tree (30 分) Suppose that all the keys in a binary tree are distinct positive integers. A unique binary tree can be determined by a given pair of postorder and inorder traversal sequences. And it is a simple standard routine to pr

1127 ZigZagging on a Tree (30 分)

1127 ZigZagging on a Tree (30 分) Suppose that all the keys in a binary tree are distinct positive integers. A unique binary tree can be determined by a given pair of postorder and inorder traversal sequences. And it is a simple standard routine to pr

PAT 1127 ZigZagging on a Tree (30)

Suppose that all the keys in a binary tree are distinct positive integers. A unique binary tree can be determined by a given pair of postorder and inorder traversal sequences. And it is a simple standard routine to print the numbers in level-order. H

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

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

PAT_A1127#ZigZagging on a Tree

Source: PAT A1127 ZigZagging on a Tree (30 分) Description: Suppose that all the keys in a binary tree are distinct positive integers. A unique binary tree can be determined by a given pair of postorder and inorder traversal sequences. And it is a sim

PAT甲级——A1127 ZigZagging on a Tree【30】

Suppose that all the keys in a binary tree are distinct positive integers. A unique binary tree can be determined by a given pair of postorder and inorder traversal sequences. And it is a simple standard routine to print the numbers in level-order. H

根据 中序遍历 和 后序遍历构造树(Presentation)(C++)

好不容易又到周五了,周末终于可以休息休息了.写这一篇随笔只是心血来潮,下午问了一位朋友PAT考的如何,顺便看一下他考的试题,里面有最后一道题,是关于给出中序遍历和后序遍历然后求一个层次遍历.等等,我找一下链接出来...... 1127. ZigZagging on a Tree (30):https://www.patest.cn/contests/pat-a-practise/1127 突然想起以前学数据结构的时候如果给出一个中序遍历和一个后序遍历然后让你画出树的结构或求出先序遍历之类的题目,

PAT甲级目录

树 备注 1004 Counting Leaves   1020 Tree Traversals   1043 Is It a Binary Search Tree  判断BST,BST的性质 1053 Path of Equal Weight   1064 Complete Binary Search Tree  完全二叉树的顺序存储,BST的性质 1066 Root of AVL Tree  构建AVL树,模板题,需理解记忆 1079 Total Sales of Supply Chain

A题目

1 1001 A+B Format(20) 2 1002 A+B for Polynomials(25) 3 1003 Emergency(25) 4 1004 Counting Leaves(30) 5 1005 Spell It Right(20) 6 1006 Sign In and Sign Out(25) 7 1007 Maximum Subsequence Sum(25) 8 1008 Elevator(20) 9 1009 Product of Polynomials(25) 10