The following is from Max Howell @twitter:

Google: 90% of our engineers use the software you wrote (Homebrew), but you can't invert a binary tree on a whiteboard so fuck off.

Now it's your turn to prove that YOU CAN invert a binary tree!

Input Specification:

Each input file contains one test case. For each case, the first line gives a positive integer N (<=10) which is the total number of nodes in the tree -- and hence the nodes are numbered from 0 to N-1. Then N lines follow, each corresponds to a node from 0 to N-1, and gives the indices of the left and right children of the node. If the child does not exist, a "-" will be put at the position. Any pair of children are separated by a space.

Output Specification:

For each test case, print in the first line the level-order, and then in the second line the in-order traversal sequences of the inverted tree. There must be exactly one space between any adjacent numbers, and no extra space at the end of the line.

Sample Input:

8
1 -
- -
0 -
2 7
- -
- -
5 -
4 6

Sample Output:

3 7 2 6 4 0 5 1
6 5 7 4 3 2 0 1
 #include<stdio.h>
#include<string>
#include<iostream>
#include<string.h>
#include<sstream>
#include<vector>
#include<map>
#include<stdlib.h>
#include<queue>
using namespace std; struct node
{
node():l(-),r(-){}
int l,r,id;
}; node Tree[];
bool notroot[];
bool fir = ;
void inoder(int root)
{
if(Tree[root].l != -)
{
inoder(Tree[root].l);
}
if(fir)
{
fir = ;
printf("%d",root);
}
else printf(" %d",root);
if(Tree[root].r != -)
{
inoder(Tree[root].r);
}
}
int main()
{
int n,tem;
char l[],r[];
scanf("%d",&n);
for(int i = ;i <n;++i)
{
Tree[i].id = i;
}
for(int i = ;i <n;++i)
{
scanf("%s%s",r,l);
if(l[] != '-')
{
tem = atoi(l);
Tree[i].l = tem;
notroot[tem] = ;
}
if(r[] != '-')
{
tem = atoi(r);
Tree[i].r = atoi(r);
notroot[tem] = ;
}
}
int root;
for(int i = ;i <n;++i)
{
if(!notroot[i])
{
root = i;
break;
}
}
queue<node> qq;
qq.push(Tree[root]);
bool fir2 = ;
while(!qq.empty())
{
node ntem = qq.front();
qq.pop();
if(fir2)
{
fir2 = ;
printf("%d",ntem.id);
}
else printf(" %d",ntem.id);
if(ntem.l != -)
{
qq.push(Tree[ntem.l]);
}
if(ntem.r != -)
{
qq.push(Tree[ntem.r]);
}
}
printf("\n");
inoder(root);
printf("\n");
return ;
}

1102. Invert a Binary Tree (25)的更多相关文章

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

  2. PAT (Advanced Level) 1102. Invert a Binary Tree (25)

    简单题. #include<cstdio> #include<cstring> #include<cmath> #include<vector> #in ...

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

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

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

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

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

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

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

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

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

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

  8. PAT 1102 Invert a Binary Tree

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

  9. 1102 Invert a Binary Tree (25 分)(二叉树遍历)

    二叉树有N个结点,给出每个结点的左右孩子结点的编号,把二叉树反转(左右孩子交换  所以是后序遍历交换) 输出反转后二叉树的层序遍历和中序遍历 #include<bits/stdc++.h> ...

随机推荐

  1. Linux Centos 7 使用yum安装 mysql5.7 (实验成功)

    第一部分:安装Mysql5.7 1.下载YUM库 shell > wget http://dev.mysql.com/get/mysql57-community-release-el7-7.no ...

  2. VMware系统运维(五)安装SSO vCenter Single Sign-On

    1.前面我们做了很多准备工作,安装了很多需求部件,这时候再安装,必备条件无,这是简单安装,即自动安装,点击"安装". 2.简单安装,提示内存不足,需要4GB以上,加内存,重新安装. ...

  3. angularJS图表-angular-flot

    1.首先需要在项目中引入的js文件有 <script src="http://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.1/jquery.mi ...

  4. HDU 1875 畅通工程再续 (最小生成树)

    畅通工程再续 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Subm ...

  5. 非常棒的 「Sublime Text 配色/主题」与「编程字体」

    *标有 CT 的是配色 **主题中调用的字体和相配套的Sublime主程序图标可访问GitHub获取 Afterglow https://github.com/YabataDesign/aftergl ...

  6. 【转】华为Java编程军规,每季度代码验收标准

    引言: 这个标准是衡量代码本身的缺陷,也是衡量一个研发人员本身的价值. 军规一:[避免在程序中使用魔鬼数字,必须用有意义的常量来标识.] 军规二:[明确方法的功能,一个方法仅完成一个功能.] 军规三: ...

  7. Meta也很强

    <!--http-equiv 必要属性--> <meta http-equiv="Content-Type" content="text/html; c ...

  8. JQuery获取append后的动态元素

    在使用ajax请求后,使用jquery将数据append到网页中.发现jquery无法使用append内的id和class获取元素. 例如:$("ul").append(" ...

  9. Ubuntu 16.04 Vysor 破解 和黑屏问题解决+ 闪屏问题解决

    最新破解更新说明: 参考本人blog: 点我呀 黑屏解决 Vysor使用和黑屏问题 经过了一段时间的艰辛探索,确定是我chrome的PNaCl没有安装,然后又是一段艰辛的Google之后,终于在一个链 ...

  10. 十八、Android引导界面

    一.所需素材 很有必要整理一下,里面附带友盟的社会化分享组件,我就不去掉了. 二.代码 import com.umeng.update.UmengUpdateAgent; import android ...