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. 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

题目大意:通过中序遍历和后序遍历构建一棵树, 并交替的输出层序遍历;思路:dfs()构建这棵树, levelOrder()交替的层序遍历这棵树;   用两个二维数组来记录层序遍历, level1记录从右到左边的遍历, level2用来记录从左到右的遍历    设置两个计数cnt1,cnt2来记录level1和了level2的位置注意点:应该对建立树的数组进行初始化,初始化为负数, 若不初始化, 默认值是1, 然而数组有下标是0,会导致第一位数不能遍历,导致错误
 1 #include<iostream>
 2 #include<vector>
 3 #include<queue>
 4 using namespace std;
 5 vector<int> in(30), post(30), level1[30], level2[30];
 6 int v[30][2], root;
 7 void dfs(int &index, int inl, int inr, int postl, int postr){
 8   int temp=post[postr], i=inl;
 9   if(inl>inr) return;
10   index = postr;
11   while(i<=inr && in[i]!=temp) i++;
12   dfs(v[index][0], inl, i-1, postl, postl+(i-inl)-1);
13   dfs(v[index][1], i+1, inr, postl+i-inl, postr-1);
14 }
15
16 void levelOrder(){
17   queue<int> q1, q2;
18   q1.push(root);
19   int cnt1=0, cnt2=0;
20   while(q1.size() || q2.size()){
21       int temp, flag1=0, flag2=0;
22       while(q1.size()){
23         temp=q1.front();
24         q1.pop();
25         level1[cnt1].push_back(post[temp]);
26         if(v[temp][0]!=-1) q2.push(v[temp][0]);
27         if(v[temp][1]!=-1) q2.push(v[temp][1]);
28         flag1=1;
29       }
30       if(flag1) cnt1++;
31       while(q2.size()){
32         temp=q2.front();
33         q2.pop();
34         level2[cnt2].push_back(post[temp]);
35         if(v[temp][0]!=-1) q1.push(v[temp][0]);
36         if(v[temp][1]!=-1) q1.push(v[temp][1]);
37         flag2=1;
38       }
39       if(flag2) cnt2++;
40   }
41 }
42 int main(){
43   int n, i, j;
44   cin>>n;
45   fill(v[0], v[0]+60, -1);
46   for(i=0; i<n; i++) cin>>in[i];
47   for(i=0; i<n; i++) cin>>post[i];
48   dfs(root, 0, n-1, 0, n-1);
49   levelOrder();
50   cout<<level1[0][0];
51   for(i=1; i<n; i++)
52       if(i%2==1) for(j=0; j<level2[i/2].size(); j++) cout<<" "<<level2[i/2][j];
53       else for(j=level1[i/2].size()-1; j>=0; j--) cout<<" "<<level1[i/2][j];
54   return 0;
55 }

原文地址:https://www.cnblogs.com/mr-stn/p/9214176.html

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

PAT 1127 ZigZagging on a Tree (30)的相关文章

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)-中序、后序建树

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

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

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

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

1064. Complete Binary Search Tree (30)【二叉树】——PAT (Advanced Level) Practise

题目信息 1064. Complete Binary Search Tree (30) 时间限制100 ms 内存限制65536 kB 代码长度限制16000 B A Binary Search Tree (BST) is recursively defined as a binary tree which has the following properties: The left subtree of a node contains only nodes with keys less tha

1099. Build A Binary Search Tree (30)【二叉树】——PAT (Advanced Level) Practise

题目信息 1099. Build A Binary Search Tree (30) 时间限制100 ms 内存限制65536 kB 代码长度限制16000 B A Binary Search Tree (BST) is recursively defined as a binary tree which has the following properties: The left subtree of a node contains only nodes with keys less than

PAT 1099. Build A Binary Search Tree (30)

1099. Build A Binary Search Tree (30) A Binary Search Tree (BST) is recursively defined as a binary tree which has the following properties: The left subtree of a node contains only nodes with keys less than the node's key. The right subtree of a nod

PAT Advanced Level 1064 Complete Binary Search Tree (30)(30 分)

1064 Complete Binary Search Tree (30)(30 分) A Binary Search Tree (BST) is recursively defined as a binary tree which has the following properties: The left subtree of a node contains only nodes with keys less than the node's key. The right subtree of