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

由后序和中序确定二叉树。然后就随便就能输出来了。
 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 int n;
 4 struct Node
 5 {
 6     int val;
 7     Node *left, *right;
 8 }*root;
 9 int inorder[50], post[50];
10 struct mat{
11     int val, ord, pri;
12 };
13 bool cmp(mat &a, mat &b){
14     return a.ord < b.ord;
15 }
16 vector<mat> vt;
17 vector<int> v;
18 Node *create(int x, int ll, int rr){
19     Node *node = (Node*)malloc(sizeof(Node));
20     node->val = post[x];
21     node->left = NULL, node->right = NULL;
22     int mid = 0;
23     for(int i = ll; i <= rr; i++){
24         if(inorder[i] == post[x]){
25             mid = i;
26             break;
27         }
28     }
29     if(ll < mid){
30         node->left = create(x-rr+mid-1, ll, mid-1);
31     }
32     if(rr > mid){
33         node->right = create(x-1, mid+1, rr);
34     }
35     return node;
36 }
37
38 void output(Node *root, int x, int y){
39     if(root != NULL){
40         output(root->left, x<<1, y+1);
41         vt.push_back({root->val, x, y});
42         output(root->right, (x<<1)+1, y+1);
43     }
44 }
45
46 int main(){
47     cin >> n;
48     for(int i = 0; i < n; i++){
49         cin >> inorder[i];
50     }
51     for(int i = 0; i < n; i++){
52         cin >> post[i];
53     }
54     root = create(n-1, 0, n-1);
55     output(root, 1, 1);
56     sort(vt.begin(), vt.end(), cmp);
57     for(int i = 0; i < vt.size(); i++){
58         if(vt[i].pri%2 == 0){
59             if(vt[i].pri != vt[i-1].pri){
60                 int j = i-1;
61                 while(vt[j].pri == vt[i-1].pri){
62                     printf("%d ", vt[j].val);
63                     j--;
64                 }
65             }
66             printf("%d%c",vt[i].val, i != n-1?‘ ‘:‘\n‘);
67         }
68     }
69     int j = vt.size()-1;
70     if(vt[j].pri%2 == 1){
71         while(vt[j].pri == vt[n-1].pri){
72             v.push_back(vt[j].val);
73             j--;
74         }
75         for(int i = 0; i < v.size(); i++){
76             printf("%d%c", v[i], i == v.size()-1?‘\n‘:‘ ‘);
77         }
78     }
79     return 0;
80 }


原文地址:https://www.cnblogs.com/zllwxm123/p/11324572.html

时间: 2024-10-05 23:47:49

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

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

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

04-树6 Complete 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 node contains only nodes with keys greate

【PAT甲级】1099 Build A Binary Search Tree (30 分)

题意: 输入一个正整数N(<=100),接着输入N行每行包括0~N-1结点的左右子结点,接着输入一行N个数表示数的结点值.输出这颗二叉排序树的层次遍历. AAAAAccepted code: 1 #define HAVE_STRUCT_TIMESPEC 2 #include<bits/stdc++.h> 3 using namespace std; 4 pair<int,int>a[107]; 5 int b[107]; 6 int cnt=0; 7 int ans[107]

PAT Advanced 1064 Complete 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 node contains only nodes with keys greate

PAT Advanced 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 node contains only nodes with keys greate

7-7 Complete 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 node contains only nodes with keys greate

1127 ZigZagging on a Tree

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