PAT_A1127#ZigZagging on a Tree
Source:
Description:
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
Keys:
Attention:
- 开始方向弄反了-,-
Code:
- /*
- Data: 2019-05-31 21:17:07
- Problem: PAT_A1127#ZigZagging on a Tree
- AC: 33:50
- 题目大意:
- 假设树的键值为不同的正整数;
- 给出二叉树的中序和后序遍历,输出二叉树的“之字形”层次遍历;
- 基本思路:
- 建树,层次遍历,依次用队列和堆存储结点值,输出即可
- */
- #include<cstdio>
- #include<stack>
- #include<queue>
- using namespace std;
- const int M=;
- int in[M],post[M];
- struct node
- {
- int data,layer;
- node *lchild,*rchild;
- };
- node *Create(int postL, int postR, int inL, int inR)
- {
- if(postL > postR)
- return NULL;
- node *root = new node;
- root->data = post[postR];
- int k;
- for(k=inL; k<=inR; k++)
- if(in[k]==root->data)
- break;
- int numLeft = k-inL;
- root->lchild = Create(postL, postL+numLeft-, inL,k-);
- root->rchild = Create(postL+numLeft, postR-, k+,inR);
- return root;
- }
- void Travel(node *root)
- {
- queue<node*> q;
- stack<node*> s;
- root->layer=;
- q.push(root);
- while(!q.empty())
- {
- root = q.front();q.pop();
- if(root->layer%==)
- {
- while(!s.empty())
- {
- printf(" %d", s.top()->data);
- s.pop();
- }
- printf(" %d", root->data);
- }
- else{
- if(root->layer==)
- printf("%d", root->data);
- else
- s.push(root);
- }
- if(root->lchild){
- root->lchild->layer=root->layer+;
- q.push(root->lchild);
- }
- if(root->rchild){
- root->rchild->layer=root->layer+;
- q.push(root->rchild);
- }
- }
- while(!s.empty())
- {
- printf(" %d", s.top()->data);
- s.pop();
- }
- }
- int main()
- {
- #ifdef ONLINE_JUDGE
- #else
- freopen("Test.txt", "r", stdin);
- #endif
- int n;
- scanf("%d", &n);
- for(int i=; i<n; i++)
- scanf("%d", &in[i]);
- for(int i=; i<n; i++)
- scanf("%d", &post[i]);
- node *root = Create(,n-,,n-);
- Travel(root);
- return ;
- }
PAT_A1127#ZigZagging on a Tree的更多相关文章
- PAT1127:ZigZagging on a Tree
1127. ZigZagging on a Tree (30) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue ...
- 1127 ZigZagging on a Tree (30 分)
1127 ZigZagging on a Tree (30 分) Suppose that all the keys in a binary tree are distinct positive in ...
- PAT甲级 1127. ZigZagging on a Tree (30)
1127. ZigZagging on a Tree (30) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue ...
- PAT甲级1127. ZigZagging on a Tree
PAT甲级1127. ZigZagging on a Tree 题意: 假设二叉树中的所有键都是不同的正整数.一个唯一的二叉树可以通过给定的一对后序和顺序遍历序列来确定.这是一个简单的标准程序,可以按 ...
- PAT 1127 ZigZagging on a Tree[难]
1127 ZigZagging on a Tree (30 分) Suppose that all the keys in a binary tree are distinct positive in ...
- pat 甲级 1127. ZigZagging on a Tree (30)
1127. ZigZagging on a Tree (30) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue ...
- A1127. ZigZagging on a Tree
Suppose that all the keys in a binary tree are distinct positive integers. A unique binary tree can ...
- 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 ...
- PAT 甲级 1127 ZigZagging on a Tree
https://pintia.cn/problem-sets/994805342720868352/problems/994805349394006016 Suppose that all the k ...
随机推荐
- CF #324 DIV2 E题
这题很简单,把目标位置排序,把目标位置在当前位置前面的往前交换,每次都是贪心选择第一个满足这样要求的数字. #include <iostream> #include <cstdio& ...
- 瀑布流 ajax 预载入 json
pbl.json[模拟后台json数据]: [ { "id": "511895", "title": ...
- Bag标签之删除书包中的一条数据
删除书包中的一条数据 查询 <esql module=help id=list> Select ID,Subject,Writer,DayTime From Messages </e ...
- scikit-learn:通过Non-negative matrix factorization (NMF or NNMF)实现LSA(隐含语义分析)
之前写过两篇文章.各自是 1)矩阵分解的综述:scikit-learn:2.5.矩阵因子分解问题 2)关于TruncatedSVD的简介:scikit-learn:通过TruncatedSVD实现LS ...
- 刚開始学习的人非常有用之chm结尾的參考手冊打开后无法正常显示
从网上下载了struts2的參考手冊.chm(本文适用全部已.chm结尾的文件)不能正常打开使用. 如图: watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/ ...
- 64位oracle数据库用32位plsql developer无法连接问题(无法载入oci.dll)
在64位操作系统下安装oracle数据库,新下载了64位数据库(假设是32位数据库安装在64位的操作系统上,无论是client还是server端.都不要去选择C:\Program Files (x86 ...
- C#操作INI文件(明天陪你看海)
C#操作INI文件 在很多的程序中,我们都会看到有以.ini为后缀名的文件,这个文件可以很方便的对程序配置的一些信息进行设置和读取,比如说我们在做一个程序后台登陆的时候,需要自动登录或者是远程配置数据 ...
- sklearn中的数据预处理和特征工程
小伙伴们大家好~o( ̄▽ ̄)ブ,沉寂了这么久我又出来啦,这次先不翻译优质的文章了,这次我们回到Python中的机器学习,看一下Sklearn中的数据预处理和特征工程,老规矩还是先强调一下我的开发环境是 ...
- [转]"RDLC"报表-参数传递及主从报表
本文转自:http://www.cnblogs.com/yjmyzz/archive/2011/09/19/2180940.html 今天继续学习RDLC报表的“参数传递”及“主从报表” 一.先创建D ...
- protobuf 编译 java js文件详解
首先下载protobuf.exe 下载地址:https://download.csdn.net/download/qq_34756156/10220137 MessageBody.proto synt ...