1127 ZigZagging on a Tree
题目:
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
题目大意:给出一个树的中序和后序遍历结果,求它的Z字型层序遍历,也就是偶数层从右往左,奇数层从左往右遍历
思路:
用一个变量保存每个结点的深度,结果按深度层层输出,正向或反向输出每一层的所有结点。
代码:(满分)
#include<stdio.h> #include<iostream> #include<queue> using namespace std; int n; int in[35], post[35]; bool flag = false, dir = false; struct Node{ int w, level; Node *lchild, *rchild; }; vector<int> level[35]; Node *build(int inl, int inr, int postl, int postr){ if(postl > postr){ return NULL; } Node *root = new Node; root -> w = post[postr]; int k; for(int i = inl; i<=inr;i++){ if(in[i] == post[postr]){ k = i; break; } } root -> lchild = build(inl, k - 1, postl, postl + k - inl - 1); root -> rchild = build(k + 1, inr, postl + k - inl, postr - 1); return root; } void levelOrder(Node *root){ queue<Node*> q; q.push(root); while(!q.empty()){ Node *tmp = q.front(); q.pop(); Node·*l·=·tmp·->·lchild; Node *r = tmp -> rchild; if(l != NULL){ l -> level = tmp -> level + 1; q.push(l); level[l -> level].push_back(l -> w); } if(r != NULL){ r -> level = tmp -> level + 1; q.push(r); level[r -> level].push_back(r -> w); } } } 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", &post[i]); } Node *root = build(0, n - 1, 0, n - 1); root -> level = 0; levelOrder(root); printf("%d", root->w); for(int i = 0; i < 35; i++){ if(dir){ for(int j = 0; j < level[i].size(); j++){ printf(" %d", level[i][j]); } dir = false; }else{ for(int j = level[i].size() - 1; j >=0; j--){ printf(" %d", level[i][j]); } dir = true; } } return 0; }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 三行代码完成国际化适配,妙~啊~
· .NET Core 中如何实现缓存的预热?