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. 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 <iostream>
 2 #include <vector>
 3 #include <queue>
 4 #include <algorithm>
 5 using namespace std;
 6 struct Node
 7 {
 8     int v;
 9     Node *l, *r;
10     Node(int a = -1) :v(a), l(nullptr), r(nullptr) {}
11 };
12 int n;
13 vector<int>inOrder, posOrder, zigOrder;
14 Node* reCreatTree(int inL, int inR, int posL, int posR)
15 {
16     if (posL > posR)
17         return nullptr;
18     Node* root = new Node(posOrder[posR]);
19     int k = inL;
20     while (k<=inR && inOrder[k] != posOrder[posR])++k;//找到根节点
21     int numL = k - inL;//左子树节点个数
22     root->l = reCreatTree(inL, k - 1, posL, posL + numL - 1);
23     root->r = reCreatTree(k + 1, inR, posL + numL, posR - 1);
24     return root;
25 }
26 void zigOrderTravel(Node* root)
27 {
28     if (root == nullptr)
29         return;
30     queue<Node*>q;
31     q.push(root);
32     zigOrder.push_back(root->v);
33     bool flag = false;
34     while (!q.empty())
35     {
36         queue<Node*>temp;
37         vector<int>tt;
38         while (!q.empty())
39         {
40             Node* p = q.front();
41             q.pop();
42             if (p->l != nullptr)
43             {
44                 temp.push(p->l);
45                 tt.push_back(p->l->v);
46             }
47             if (p->r != nullptr)
48             {
49                 temp.push(p->r);
50                 tt.push_back(p->r->v);
51             }
52         }
53         if (flag)
54             reverse(tt.begin(), tt.end());
55         zigOrder.insert(zigOrder.end(), tt.begin(), tt.end());
56         flag = !flag;
57         q = temp;
58     }
59 }
60 int main()
61 {
62     cin >> n;
63     inOrder.resize(n);
64     posOrder.resize(n);
65     for (int i = 0; i < n; ++i)
66         cin >> inOrder[i];
67     for (int i = 0; i < n; ++i)
68         cin >> posOrder[i];
69     Node* root = reCreatTree(0, n - 1, 0, n - 1);
70     zigOrderTravel(root);
71     for (int i = 0; i < zigOrder.size(); ++i)
72         cout << (i > 0 ? " " : "") << zigOrder[i];
73     return 0;
74 }

原文地址:https://www.cnblogs.com/zzw1024/p/11478533.html

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

PAT甲级——A1127 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 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 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 甲级 1066 Root of AVL Tree

https://pintia.cn/problem-sets/994805342720868352/problems/994805404939173888 An AVL tree is a self-balancing binary search tree. In an AVL tree, the heights of the two child subtrees of any node differ by at most one; if at any time they differ by m

PAT 甲级 1034 Head of a Gang (30 分)(bfs,map,强连通)

1034 Head of a Gang (30 分) One way that the police finds the head of a gang is to check people's phone calls. If there is a phone call between A and B, we say that A and B is related. The weight of a relation is defined to be the total time length of

PAT 甲级 1066 Root of AVL Tree (25 分)(快速掌握平衡二叉树的旋转,内含代码和注解)***

1066 Root of AVL Tree (25 分) An AVL tree is a self-balancing binary search tree. In an AVL tree, the heights of the two child subtrees of any node differ by at most one; if at any time they differ by more than one, rebalancing is done to restore this

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甲级】1110 Complete Binary Tree (25分)

题意: 输入一个正整数N(<=20),代表结点个数(0~N-1),接着输入N行每行包括每个结点的左右子结点,'-'表示无该子结点,输出是否是一颗完全二叉树,是的话输出最后一个子结点否则输出根节点. trick: 用char输入子结点没有考虑两位数的结点??... stoi(x)可以将x转化为十进制整数 AAAAAccepted code: 1 #define HAVE_STRUCT_TIMESPEC 2 #include<bits/stdc++.h> 3 using namespace