您的位置:首页 > 其它

PAT (Advanced Level) 1127. ZigZagging on a Tree (30)

2017-03-06 22:35 344 查看


1127. ZigZagging on a Tree (30)

时间限制

400 ms

内存限制

65536 kB

代码长度限制

16000 B

判题程序

Standard

作者

CHEN, Yue

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


这道题今晚终于AC了,相比前面两道看不懂的25分的题目,这道题就是传说中的,看得懂但是不会做的类型。。。。

所以我在想,除非我做过类似的题目,不然我是不可能在那么短的考试时间里面写出来并且AC的。。。

这道题,暴力算确实可以,而且我也百度了,看见有些大佬是用双向链表,有的说用vector<vector<int> > a;来解决,鉴于我目前对vector掌握的还不是很熟练,所以我想了一晚上,终于想到用栈结合队列,然后把他们一起放进BFS里面来输出~

想到用栈,是因为,题目要求是反方向的Z字型输出,然后刚开始也是考虑在create树的时候,记录他们每个点(node)的层值,

然后判断奇偶性来输出,但是后来觉得为什么不用BFS输出呢?

比如只要在奇数层的时候(从1开始),判断当前队列是否还有数字,如果还有就说明是上一层偶数层的数字还没有完全输出,此时要耐心“等候”,也就是要将他们的左右孩子们读入栈里面,直到判断到队列已经空啦,队列要求要加入新的数字的时候,我们再将栈里面的数值“一口气”全部输出~~在偶数层也是类似的道理,但是要注意在将奇数层的数字加入队列的时候,要先加入右孩子,偶数层要先加入左孩子~~(这是因为反方向Z字决定的性质)

下面就是代码~~

#include<cstdio>
#include<queue>
#include<stack>
#include<string.h>
#include<algorithm>
#include<math.h>

using namespace std;

const int maxn = 35;

int n;
int f = 0;
int xx = 0;

int pos[maxn], in[maxn];

struct node {
int data;
int layer;
node* lchild;
node* rchild;
};

node* create(int posL, int posR, int inL, int inR, int ll) {
node* root = new node;

if (posL > posR) {
return NULL;
}

root->data = pos[posR];
root->layer = ll;

int k;
for (k = inL; k <= inR; k++) {
if (in[k] == pos[posR]) {
break;
}
}

int num = k - inL;
root->lchild = create(posL, posL + num - 1, inL, k - 1, ll + 1);
root->rchild = create(posL + num, posR - 1, k + 1, inR, ll + 1);
return root;
}

void BFS(node* root) {
stack<node*> sss;

queue<node*> q;
q.push(root);

while (!q.empty()) {
node* top = q.front();
q.pop();

if (f != n - 1) {
printf("%d ", top->data);
f++;
}
else {
printf("%d\n", top->data);
break;
}

if ((top->layer) % 2 != 0) {
if (top->rchild != NULL) { sss.push(top->rchild); }
if (top->lchild != NULL) { sss.push(top->lchild); }

if (q.empty()) {
while (!sss.empty()) {
node* temp = sss.top();
sss.pop();
q.push(temp);
}
}
}
else {
if (top->lchild != NULL) {
sss.push(top->lchild);
}
if (top->rchild != NULL) {
sss.push(top->rchild);
}

if (q.empty()) {
while (!sss.empty()) {
node* temp = sss.top();
sss.pop();
q.push(temp);
}
}

}
}
}

int main() {
scanf("%d", &n);
for (int i = 0; i < n; i++) {
scanf("%d", &in[i]);
}
for (int i = 0; i < n; i++) {
scanf("%d", &pos[i]);
}
node* root = create(0, n - 1, 0, n - 1, 1);
BFS(root);
return 0;
}




                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: