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 ( ...
随机推荐
- 用 rebar 来构建、编译、测试、发布 Erlang 应用程序
转自:http://dhq.me/build-compile-eunit-release-erlang-application-with-rebar rebar 是一个遵循 Erlang/OTP 原则 ...
- Unlocker(强力删除文件工具) 1.9.2 汉化绿色版
软件名称: Unlocker(强力删除文件工具) 1.9.2 汉化绿色版软件语言: 简体中文授权方式: 免费软件运行环境: Win7 / Vista / Win2003 / WinXP 软件大小: 5 ...
- python--zeros函数和ones函数
使用numpy.zeros,numpy.ones,numpy.eye等方法可以构造特定的矩阵 例如: 代码如下: >>>from numpy import * >>> ...
- Python 学习笔记4
我是一个艺术家. 今天继续学习python啊.争取看到python流程控制.
- BJFU 1397 致我们终将逝去的爱情
LIS 由于要记录轨迹,所以不能用O(nlogn)优化,直接dp加father记录每个节点的转移. #include<cstdio> #include<algorithm> ...
- Python 修炼1
2016年11月21日 Python基础修炼第一篇 1.Python是什么?有什么优缺点呢? python是一个高级编程语言. 优点:开发效率比较高,不但有php写网页的功能,还有写后台的功能 缺点: ...
- 修改jsonb的属性
CREATE FUNCTION jsonb_merge(JSONB, JSONB) RETURNS JSONB AS $$ WITH json_union AS ( SELECT * FROM JSO ...
- drawableLayout的使用(转载讲的比较清晰的文章)
创建drawbler的布局文件 初始化drawbler的列表 响应drawable列表点击事件 现在侧滑菜单使用很多,大都是通过SlidingMenu实现.现在也可以通过DrawerLayout 创建 ...
- mongodb 数据库导入.cvs文件时某些字段类型变成NumberLong的解决办法
在mongodb中导入数据时,会在数据库中生成字段记录为NumberLong的数据,可以使用以下方式将其转换为String db.Account.find().forEach( function(it ...
- ASP.NET MVC View向Controller提交数据
我们知道使用MVC的一个很重的的用途就是把Controller和View之间进行解耦,通过控制器来调用不同的视图,这就注定了Controller和View之间的传值是一个很重的知识点,这篇博文主要解释 ...