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. Java Script基础(十二) 正则表达式

    一.正则表达式中常用的符号 虽然可以使用string函数来完成验证,但是这种验证方式不够严谨,而且编写比较麻烦.而正则表达式是一种描述字符模式的对象,由一些特殊的符号组成,其组成的字母模式用来匹配各种 ...

  2. 存储过程 <3> 和函数的区别

    二.函数和存储过程的优点: 1.共同使用的代码可以只需要被编写一次,而被需要该代码的任何应用程序调用(.net,c++,java,也可以使DLL库). 2.这种几种编写.几种维护更新.大家共享的方法, ...

  3. Daily Query

    -- GI Report SELECT A.PLPKLNBR, D.DNDNHNBR, F.DNSAPCPO, C.PPPRODTE, A.GNUPDDTE GI_DATE, B.INHLDCDE, ...

  4. CentOS7,Firewalld防火墙使用方法

    查看防火墙状态 systemctl status firewalld.service 启动firewall systemctl stop firewalld.service 停止firewall sy ...

  5. crawler4j:轻量级多线程网络爬虫实例

    crawler4j是Java实现的开源网络爬虫.提供了简单易用的接口,可以在几分钟内创建一个多线程网络爬虫. 下面实例结合jsoup(中文版API),javacvs 爬取自如租房网(http://sh ...

  6. You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '

    mysql中如果字段使用了关键字,在插入和更新时会提示 You have an error in your SQL syntax; check the manual that corresponds ...

  7. 【Mongodb】---Scheme和Collections对应问题

    Mongodb通过mongoose来与数据进行操作.而mongoose是通过model来创建数据库中对应的collection mongoose.model('User', UserSchema); ...

  8. Linux下设置定期执行脚本

    下面针对的是非ubuntu环境,会在文章末尾介绍ubuntu的一些区别. 在Linux下,经常需要定期的执行一些脚本从而来实现一些功能. 在Linux下我们用crontab来实现定期的执行脚本这个功能 ...

  9. ASP.NET缓存全解析7:第三方分布式缓存解决方案 Memcached和Cacheman 转自网络原文作者李天平

    Memcached — 分布式缓存系统 1.Memcached是什么? Memcached是高性能的,分布式的内存对象缓存系统,用于在动态应用中减少数据库负载,提升访问速度.Memcached通过在内 ...

  10. static和public

    static:静态.   可以设置:静态类.静态变量.静态方法.   没有使用static修饰的成员为实例成员. 静态成员的使用:通过类名.   1.不加static修饰的成员是对象成员,归每个对象所 ...