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 simple standard routine to print the numbers in level-order. However, if you think the problem is too simple, then you are too naive. This time you are supposed to print the numbers in "zigzagging order" -- that is, starting from the root, print the numbers level-by-level, alternating between left to right and right to left. For example, for the following tree you must output: 1 11 5 8 17 12 20 15.

Input Specification:

Each input file contains one test case. For each case, the first line gives a positive integer N (≤30), the total number of nodes in the binary tree. The second line gives the inorder sequence and the third line gives the postorder sequence. All the numbers in a line are separated by a space.

Output Specification:

For each test case, print the zigzagging sequence of the tree in a line. All the numbers in a line must be separated by exactly one space, and there must be no extra space at the end of the line.

Sample Input:

8
12 11 20 17 1 15 8 5
12 20 17 11 15 8 5 1

Sample Output:

1 11 5 8 17 12 20 15

Keys:

  • 二叉树的建立
  • 二叉树的遍历

Attention:

  • 开始方向弄反了-,-

Code:

 1 /*
 2 Data: 2019-05-31 21:17:07
 3 Problem: PAT_A1127#ZigZagging on a Tree
 4 AC: 33:50
 5
 6 题目大意:
 7 假设树的键值为不同的正整数;
 8 给出二叉树的中序和后序遍历,输出二叉树的“之字形”层次遍历;
 9 */
10
11 #include<cstdio>
12 #include<stack>
13 #include<queue>
14 using namespace std;
15 const int M=35;
16 int in[M],post[M];
17 struct node
18 {
19     int data,layer;
20     node *lchild,*rchild;
21 };
22
23 node *Create(int postL, int postR, int inL, int inR)
24 {
25     if(postL > postR)
26         return NULL;
27     node *root = new node;
28     root->data = post[postR];
29     int k;
30     for(k=inL; k<=inR; k++)
31         if(in[k]==root->data)
32             break;
33     int numLeft = k-inL;
34     root->lchild = Create(postL, postL+numLeft-1, inL,k-1);
35     root->rchild = Create(postL+numLeft, postR-1, k+1,inR);
36     return root;
37 }
38
39 void Travel(node *root)
40 {
41     queue<node*> q;
42     stack<node*> s;
43     root->layer=1;
44     q.push(root);
45     while(!q.empty())
46     {
47         root = q.front();q.pop();
48         if(root->layer%2==0)
49         {
50             while(!s.empty())
51             {
52                 printf(" %d", s.top()->data);
53                 s.pop();
54             }
55             printf(" %d", root->data);
56         }
57         else{
58             if(root->layer==1)
59                 printf("%d", root->data);
60             else
61                 s.push(root);
62         }
63         if(root->lchild){
64             root->lchild->layer=root->layer+1;
65             q.push(root->lchild);
66         }
67         if(root->rchild){
68             root->rchild->layer=root->layer+1;
69             q.push(root->rchild);
70         }
71     }
72     while(!s.empty())
73     {
74         printf(" %d", s.top()->data);
75         s.pop();
76     }
77 }
78
79 int main()
80 {
81 #ifdef    ONLINE_JUDGE
82 #else
83     freopen("Test.txt", "r", stdin);
84 #endif
85
86     int n;
87     scanf("%d", &n);
88     for(int i=0; i<n; i++)
89         scanf("%d", &in[i]);
90     for(int i=0; i<n; i++)
91         scanf("%d", &post[i]);
92     node *root = Create(0,n-1,0,n-1);
93     Travel(root);
94
95     return 0;
96 }

原文地址:https://www.cnblogs.com/blue-lin/p/10957755.html

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

PAT_A1127#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甲级——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

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

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

1127 ZigZagging on a Tree

题意:中序序列+后序序列构建二叉树,之字形输出其层序序列. 思路:在结点的数据域中额外增加一个layer表示结点所在的层次,并定义vector<int> zigzag[maxn]存放最终结果.按照常规顺序进行层序遍历,将第i层的值存入到zigzag[i]中,最后输出时,第偶数层从左向右输出,第奇数层反之. 代码: #include <cstdio> #include <queue> #include <vector> using namespace std;

根据 中序遍历 和 后序遍历构造树(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