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. zoj 3742 Delivery 好题

    Delivery 题目还是自己看吧 - -! 看似图论,实际上是一个考察思维以及数据结构的题. 我们对于先前和向后的边分别进行统计. 对询问离线. 小边按照左端点从大到小排序. 1.对于向后的边,询问 ...

  2. uclibc,eglibc,glibc之间的区别和联系

    http://bbs.chinaunix.net/thread-3762882-1-1.html 1.Glibc glibc = GNU C Library 是GNU项(GNU Project)目,所 ...

  3. freeCodeCamp:Convert HTML Entities

    将字符串中的字符 &.<.>." (双引号), 以及 '(单引号)转换为它们对应的 HTML 实体. 现在这个表里找出要转化的符号https://dev.w3.org/h ...

  4. 全新jquery多点滑动幻灯片——全屏动画animateSlide

    首页banner的酷炫效果多来自全屏大图的幻灯片动画,下面提供一种完美兼容的jquery动画特效:全新jquery多点滑动幻灯片——全屏动画animateSlide(代码完全原创). 直接上代码,把h ...

  5. 【trim()】去掉字符串开头和结尾的空格,防止不必要的空格导致的错误。

    去掉字符串开头和结尾的空格,防止不必要的空格导致的错误. public static void main(String arg[]){ String a=" abc"; Strin ...

  6. php学习笔记6--php中的文件包含 include,require,include_once,require_once

    php中的文件包含 include,require,include_once,require_once 文件包含:是指将一个文件的内容包含进另外一个文件,有利于代码的复用等.php中文件包含指令有4个 ...

  7. SQL Server高级内容之子查询和表链接

    1.子查询概念 (1)就是在查询的where子句中的判断依据是另一个查询的结果,如此就构成了一个外部的查询和一个内部的查询,这个内部的查询就是自查询. (2)自查询的分类 1)独立子查询 ->独 ...

  8. C# 中怎么将string转换成int型

    int intA = 0;1.intA =int.Parse(str);2.int.TryParse(str, out intA);3.intA = Convert.ToInt32(str);以上都可 ...

  9. iOS - 使用进阶

    1. 状态栏显示风火轮 //  ViewController.m //  1.状态栏显示风火轮 // //  Created by wind on 16/11/13. //  Copyright © ...

  10. c#中SqlHelper类的编写(三)

    下面我们直接用可变长度参数的方式写一个完整的SqlHelper增删改public static int ExecuteNonQuery(string sql,params Parameter[] pa ...