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

来源:互联网 发布:收入证明 知乎 编辑:程序博客网 时间:2024/05/16 09:25

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:
812 11 20 17 1 15 8 512 20 17 11 15 8 5 1
Sample Output:

1 11 5 8 17 12 20 15

今天甲级的最后一道题,也是我顶级第一题,一开始被题目吓了一跳,以为又给我来个平衡树什么的,结果还是老套路的二叉树还原题。

直接递归还原出原树,然后做bfs的遍历就行了。

#include<map> #include<set>#include<ctime>  #include<cmath>      #include<queue>   #include<string>  #include<vector>  #include<cstdio>      #include<cstring>    #include<iostream>  #include<algorithm>      #include<functional>  using namespace std;#define ms(x,y) memset(x,y,sizeof(x))      #define rep(i,j,k) for(int i=j;i<=k;i++)      #define per(i,j,k) for(int i=j;i>=k;i--)      #define loop(i,j,k) for (int i=j;i!=-1;i=k[i])      #define inone(x) scanf("%d",&x)      #define intwo(x,y) scanf("%d%d",&x,&y)      #define inthr(x,y,z) scanf("%d%d%d",&x,&y,&z)    #define infou(x,y,z,p) scanf("%d%d%d%d",&x,&y,&z,&p)   #define lson x<<1,l,mid  #define rson x<<1|1,mid+1,r  #define mp(i,j) make_pair(i,j)  #define ft first  #define sd second  typedef long long LL;typedef pair<int, int> pii;const int low(int x) { return x&-x; }const int INF = 0x7FFFFFFF;const int mod = 1e9 + 7;const int N = 3e5 + 10;const int M = 1e4 + 1;const double eps = 1e-10;int n, a[N], b[N], ch[N][2], c[N], d[N], p[N], rt, cnt, sz;void solve(int &rt, int l, int r, int ll, int rr){  if (l > r) return;  rt = ++cnt; c[rt] = b[rr];  rep(i, l, r) if (a[i] == b[rr])  {    solve(ch[rt][0], l, i - 1, ll, ll + i - l - 1);    solve(ch[rt][1], i + 1, r, ll + i - l, rr - 1);  }}int main(){  inone(n);  rep(i, 1, n) inone(a[i]);  rep(i, 1, n) inone(b[i]);  sz = cnt = 0; solve(rt, 1, n, 1, n);  p[sz] = rt; d[sz++] = 1;  for (int i = 0, j, flag = 0; i < sz; i = j, flag ^= 1)  {    for (j = i; j < sz&&d[j] == d[i]; j++);    if (i == 0) printf("%d", c[rt]);    else if (flag) rep(k, i, j - 1) printf(" %d", c[p[k]]);    else per(k, j - 1, i) printf(" %d", c[p[k]]);    rep(k, i, j - 1)    {      if (ch[p[k]][0]) p[sz] = ch[p[k]][0], d[sz++] = d[i] + 1;      if (ch[p[k]][1]) p[sz] = ch[p[k]][1], d[sz++] = d[i] + 1;    }  }  return 0;}


0 1
原创粉丝点击