Source:

PAT A1102 Invert a Binary Tree (25 分)

Description:

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 (≤) 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

Keys:

Code:

 /*
time: 2019-06-30 14:09:56
problem: PAT_A1102#Invert a Binary Tree
AC: 23:00 题目大意:
打印镜像树层序和中序遍历
输入:
第一行给出,结点数N<=10
接下来N行,结点i(0~n-1)的左孩子和右孩子 基本思路:
构造静态树遍历
*/
#include<cstdio>
#include<queue>
#include<string>
#include<iostream>
using namespace std;
const int M=1e2;
int mp[M]={},n;
struct node
{
int lchild,rchild;
}tree[M]; void LayerOrder(int root)
{
queue<int> q;
q.push(root);
int pt=;
while(!q.empty())
{
root = q.front();
q.pop();
printf("%d%c", root, ++pt==n?'\n':' ');
if(tree[root].rchild != -)
q.push(tree[root].rchild);
if(tree[root].lchild != -)
q.push(tree[root].lchild);
}
} void InOrder(int root)
{
if(root == -)
return;
static int pt=;
InOrder(tree[root].rchild);
printf("%d%c", root, ++pt==n?'\n':' ');
InOrder(tree[root].lchild);
} int main()
{
#ifdef ONLINE_JUDGE
#else
freopen("Test.txt", "r", stdin);
#endif // ONLINE_JUDGE scanf("%d", &n);
string r,l;
for(int i=; i<n; i++){
cin >> l >> r;
if(l == "-")
tree[i].lchild = -;
else{
tree[i].lchild = atoi(l.c_str());
mp[tree[i].lchild]=;
}
if(r == "-")
tree[i].rchild = -;
else{
tree[i].rchild = atoi(r.c_str());
mp[tree[i].rchild]=;
}
}
int root;
for(int i=; i<n; i++)
if(mp[i]==)
root=i;
LayerOrder(root);
InOrder(root); return ;
}

PAT_A1102#Invert a Binary Tree的更多相关文章

  1. 1102. Invert a Binary Tree (25)

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

  2. Invert a binary tree 翻转一棵二叉树

    Invert a binary tree 翻转一棵二叉树 假设有如下一棵二叉树: 4  / \   2    7  / \   / \ 1  3 6  9翻转后: 4     /    \    7 ...

  3. PAT1102: Invert a Binary Tree

    1102. Invert a Binary Tree (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue ...

  4. PAT A1102 Invert a Binary Tree (25 分)——静态树,层序遍历,先序遍历,后序遍历

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

  5. A1102. Invert a Binary Tree

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

  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. PAT甲级——1102 Invert a Binary Tree (层序遍历+中序遍历)

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

  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. PAT甲级——A1102 Invert a Binary Tree

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

随机推荐

  1. 【LeetCode 3】无重复字符的最长子串

    描述 [题解] 尺取法 对于[l..r]这段 如果新加进来的s[r]有和之前的重复. 那么就不用重新开始了. 直接递增左区间. 让这段没有重复数字了再说. *****然后再把s[r]加进去. [代码] ...

  2. JSTL标签的用法详解

    在JSP中写Java代码是一件很恶心的事情,代码量少的话还可以,要是多的话,就蛋疼了,整个页面都是<%  %>所以EL表达式和JSTL就应运而生了,这里我们注重讲解一下JSTL标签的使用: ...

  3. vim编辑器设置缩进!

    转载自 http://blog.chinaunix.net/uid-27213819-id-3813909.html 1.在自己的home目录下建立.vimrc文件.控制台输入vi ~/.vimrc ...

  4. 【Python】- scrapy 爬取图片保存到本地、且返回保存路径

    https://blog.csdn.net/xueba8/article/details/81843534

  5. java-day26

    ## DOM简单学习:为了满足案例要求     * 功能:控制html文档的内容     * 获取页面标签(元素)对象:Element         * document.getElementByI ...

  6. 「SNOI2019」通信 分治优化费用流建图

    题意: n 个排成一列的哨站要进行通信.第 i 个哨站的频段为 ai. 每个哨站 ii 需要选择以下二者之一: 1.直接连接到控制中心,代价为 W:2.连接到前面的某个哨站 j(j<i),代价为 ...

  7. spring Aop设计原理

    转载至:https://blog.csdn.net/luanlouis/article/details/51095702 0.前言 Spring 提供了AOP(Aspect Oriented Prog ...

  8. nginx打包成rpm

    [root@localhost ~ ]#yum -y install lrzsz pcre pcre-devel zlib zlib-devel vim nrt-tools psmisc gcc gc ...

  9. 随笔记录 yum -y clean all出错解决方案

    出现以下问题的解决方案: 使用解决方案之前,要先确定一下几种情况: 1.检查光盘是否挂载 2.ISO映像文件是否使用正确 解决方案1: rm -rf /var/cache/yum/* yum repo ...

  10. PHP算法之有效的括号

    给定一个只包括 '(',')','{','}','[',']' 的字符串,判断字符串是否有效. 有效字符串需满足: 左括号必须用相同类型的右括号闭合.左括号必须以正确的顺序闭合.注意空字符串可被认为是 ...