PAT (Advanced Level) 1102. Invert a Binary Tree (25)
简单题。
#include<cstdio>
#include<cstring>
#include<cmath>
#include<vector>
#include<map>
#include<queue>
#include<stack>
#include<algorithm>
using namespace std; struct Node
{
int left;
int right;
}s[];
int n;
vector<int>ans1,ans2;
int flag[];
int root; void bfs()
{
queue<int>q; q.push(root);
while(!q.empty())
{
int h=q.front(); q.pop();
ans1.push_back(h);
if(s[h].left!=-) q.push(s[h].left);
if(s[h].right!=-) q.push(s[h].right);
}
} void dfs(int x)
{
if(s[x].left!=-) dfs(s[x].left);
ans2.push_back(x);
if(s[x].right!=-) dfs(s[x].right);
} int main()
{
memset(flag,,sizeof flag);
scanf("%d",&n);
for(int i=;i<n;i++)
{
char L[],R[];
scanf("%s%s",L,R);
if(L[]=='-') s[i].left=-;
else
{
s[i].left=L[]-'';
flag[L[]-'']=;
}
if(R[]=='-') s[i].right=-;
else
{
s[i].right=R[]-'';
flag[R[]-'']=;
}
} for(int i=;i<n;i++)
if(flag[i]==) root=i; for(int i=;i<n;i++)
swap(s[i].left,s[i].right); bfs();
dfs(root);
for(int i=;i<ans1.size();i++)
{
printf("%d",ans1[i]);
if(i<ans1.size()-) printf(" ");
else printf("\n");
} for(int i=;i<ans1.size();i++)
{
printf("%d",ans2[i]);
if(i<ans2.size()-) printf(" ");
else printf("\n");
} return ;
}
PAT (Advanced Level) 1102. Invert a Binary Tree (25)的更多相关文章
- 【PAT甲级】1102 Invert a Binary Tree (25 分)(层次遍历和中序遍历)
题意: 输入一个正整数N(<=10),接着输入0~N-1每个结点的左右儿子结点,输出这颗二叉树的反转的层次遍历和中序遍历. AAAAAccepted code: #define HAVE_STR ...
- PAT Advanced 1102 Invert a Binary Tree (25) [树的遍历]
题目 The following is from Max Howell @twitter: Google: 90% of our engineers use the sofware you wrote ...
- 1102. Invert a Binary Tree (25)
The following is from Max Howell @twitter: Google: 90% of our engineers use the software you wrote ( ...
- PAT甲题题解-1102. Invert a Binary Tree (25)-(建树,水题)
就是把输入给的左孩子右孩子互换一下,然后输出层次遍历和中序遍历. #include <iostream> #include <algorithm> #include <c ...
- PAT (Advanced Level) 1066. Root of AVL Tree (25)
AVL树的旋转.居然1A了.... 了解旋转方式之后,数据较小可以当做模拟写. #include<cstdio> #include<cstring> #include<c ...
- PAT甲级——1102 Invert a Binary Tree (层序遍历+中序遍历)
本文同步发布在CSDN:https://blog.csdn.net/weixin_44385565/article/details/90577042 1102 Invert a Binary Tree ...
- PAT 1102 Invert a Binary Tree[比较简单]
1102 Invert a Binary Tree(25 分) The following is from Max Howell @twitter: Google: 90% of our engine ...
- 1102 Invert a Binary Tree——PAT甲级真题
1102 Invert a Binary Tree The following is from Max Howell @twitter: Google: 90% of our engineers us ...
- PAT 1102 Invert a Binary Tree
The following is from Max Howell @twitter: Google: 90% of our engineers use the software you wrote ( ...
随机推荐
- C -小晴天老师系列——竖式乘法
C - 小晴天老师系列——竖式乘法 Time Limit: 4000/2000MS (Java/Others) Memory Limit: 128000/64000KB (Java/Others ...
- web跨域问题
环境: win7_x64旗舰版.Google Chrome-v44.0.2403.155.node-v0.10.29.express-v3.2.5.jQuery-v1.8.3 一.跨域GET: 客户端 ...
- Webdriver实现对菜单栏的灵活切换功能,附上代码,类似的菜单栏切换可以自己封装
有时一级菜单下可能会有二级菜单,这时就需要对其下面的元素进行判断,如果使用webdriver原生的方法去获取未知的元素进行判断,显然是不可能的,因为webdriver本身就是基于明确的元素进行定位的, ...
- UVALive 7077 Little Zu Chongzhi's Triangles (有序序列和三角形的关系)
这个题--我上来就给读错了,我以为最后是一个三角形,一条边可以由多个小棒组成,所以想到了状态压缩各种各样的东西,最后成功了--结果发现样例过不了,三条黑线就在我的脑袋上挂着,改正了以后我发现N非常小, ...
- Windows如何压缩tar.gz格式
Windows如何压缩tar.gz格式 tar.gz 是linux和unix下面比较常用的格式,几个命令就可以把文件压缩打包成tar.gz格式 然而这种格式在windows并不多见,WinRAR.Wi ...
- Chapter 2 Open Book——3
But when I walked into the cafeteria with Jessica — 但是当我和Jessica 一起走进自助餐厅的时候 trying to keep my eyes ...
- 样式的操作-访问外部定义的css样式
JS对css的控制力非常强,甚至可以控制外部定义的css样式 ———————————————————————— <style> .myclass{ ...
- Encoded Love-letter
Encoded Love-letter Time Limit : 1000/1000ms (Java/Other) Memory Limit : 32768/32768K (Java/Other) ...
- 客户端 HttpUtils.java
package com.http.post; import java.io.ByteArrayOutputStream; import java.io.IOException; import java ...
- 自动删除超过30天文件的vbs脚本【转发】
利用代码制作自动删除超过30天的文件及文件夹的vbs脚本,定期清理文件夹中长时间无用文件. 1.首先在新建一个文本文档,粘贴代码(代码可通过添加微信公众号vbs_edit(VBS脚本之家)回复018获 ...