简单题。

#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)的更多相关文章

  1. 【PAT甲级】1102 Invert a Binary Tree (25 分)(层次遍历和中序遍历)

    题意: 输入一个正整数N(<=10),接着输入0~N-1每个结点的左右儿子结点,输出这颗二叉树的反转的层次遍历和中序遍历. AAAAAccepted code: #define HAVE_STR ...

  2. 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 ...

  3. 1102. Invert a Binary Tree (25)

    The following is from Max Howell @twitter: Google: 90% of our engineers use the software you wrote ( ...

  4. PAT甲题题解-1102. Invert a Binary Tree (25)-(建树,水题)

    就是把输入给的左孩子右孩子互换一下,然后输出层次遍历和中序遍历. #include <iostream> #include <algorithm> #include <c ...

  5. PAT (Advanced Level) 1066. Root of AVL Tree (25)

    AVL树的旋转.居然1A了.... 了解旋转方式之后,数据较小可以当做模拟写. #include<cstdio> #include<cstring> #include<c ...

  6. PAT甲级——1102 Invert a Binary Tree (层序遍历+中序遍历)

    本文同步发布在CSDN:https://blog.csdn.net/weixin_44385565/article/details/90577042 1102 Invert a Binary Tree ...

  7. PAT 1102 Invert a Binary Tree[比较简单]

    1102 Invert a Binary Tree(25 分) The following is from Max Howell @twitter: Google: 90% of our engine ...

  8. 1102 Invert a Binary Tree——PAT甲级真题

    1102 Invert a Binary Tree The following is from Max Howell @twitter: Google: 90% of our engineers us ...

  9. PAT 1102 Invert a Binary Tree

    The following is from Max Howell @twitter: Google: 90% of our engineers use the software you wrote ( ...

随机推荐

  1. WEB组件 开发 (未完成 4-13)

    整理自真阿当的阿当大话西游之WEB组件,课件中的代码下载. 14. 抽出widget类 组件分两大类: utility(大部分与UI无关的组件) 和 widget(应用层,大部分与UI相关的,日历组件 ...

  2. CSS 各类 块级元素 行级元素 水平 垂直 居中问题

    元素的居中问题是每个初学者碰到的第一个大问题,在此我总结了下各种块级 行级 水平 垂直 的居中方法,并尽量给出代码实例. 首先请先明白块级元素和行级元素的区别 行级元素 一块级元素 1 水平居中: ( ...

  3. 在textarea的内容后面增加内容

    document.getElementById("result").value+=data+"\n>"; 本文为本人用来记录自己做的一些东西,如有不对的地 ...

  4. 自定义控件之--继承控件(圆形TextView)

    师从郭大,自学于心,继承控件无疑就是继承自现有控件,保持继承的控件的属性并进行必要的扩展.   比如下面这个自定义控件,它就保持了TextView的属性,并对TextView的外观进行必要的修改该来适 ...

  5. HDU 5810 Balls and Boxes

    n*(m-1)/(m*m) #pragma comment(linker, "/STACK:1024000000,1024000000") #include<cstdio&g ...

  6. Java 处理图片 base64 编码的相互转换

    哈喽大家好我是刘德华. 今天项目优化了一下上传头像的功能.采用 imagecropper 插件完成裁剪图片的效果. 这个插件裁剪完的图片都是 base64 加密的字符串,上传头像也就涉及到了如何把加密 ...

  7. C语言_愤怒的小鸟

    // ConsoleApplication4.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include<stdio.h> ...

  8. js 交换表单中值

    <html> <head> <meta charset="utf-8" /> <script type="text/javasc ...

  9. repeat a string in java

    if I want to repeat "hello" four times as a new string-> "hellohellohellohello&quo ...

  10. Gridview导出成Excel

    在aspx里面加上 <%@ Page Language="C#" AutoEventWireup="true" CodeFile="AAAAAA ...