题意:给定一个二叉树,要求输出翻转后的二叉树的层序序列和中序序列。

思路:不用真的翻转,只需要在输出时先访问右结点再访问左结点即可。

代码:

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <queue>
using namespace std;

struct Node{
    int left,right;
}tree[];
]={};
int n;

void levelOrder(int root)
{
    ;
    queue<int> q;
    q.push(root);
    while(!q.empty()){
        int u=q.front();
        q.pop();
        printf("%d",u);
        cnt++;
        if(cnt==n) printf("\n");
        else printf(" ");

        ) q.push(tree[u].right);
        ) q.push(tree[u].left);
    }
}

void inOrder(int root)
{
    ;
    ){
        inOrder(tree[root].right);
        printf("%d",root);
        k++;
        if(k==n) printf("\n");
        else printf(" ");
        inOrder(tree[root].left);
    }
}

int main()
{
    scanf("%d",&n);
    ],s2[];
    ;i<n;i++){
        scanf("%s %s",s1,s2);
        ]==;
        else {
            tree[i].left=atoi(s1);
            isRoot[tree[i].left]=;
        }
        ]==;
        else {
            tree[i].right=atoi(s2);
            isRoot[tree[i].right]=;
        }
    }
    ;
    ) root++;
    levelOrder(root);
    inOrder(root);
    ;
}

1102 Invert a Binary Tree的更多相关文章

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

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

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

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

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

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

  4. 1102. Invert a Binary Tree (25)

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

  5. PAT 1102 Invert a Binary Tree

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

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

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

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

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

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

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

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

随机推荐

  1. iOS跳转支付宝付款码和扫一扫页面

    iOS跳转支付宝付款码和扫一扫页面 // 是否支持支付宝 NSURL * myURL_APP_A = [NSURL URLWithString:@"alipay://"]; if ...

  2. Conky配置文件

    Conky是一个可以在linux系统中实时显示系统性能的工具,美观且十分好用,我们选择安装conky-all程序包 # set to yes if you want Conky to be forke ...

  3. Shell学习笔记——算数运算与条件测试

    算数运算 1. 使用let命令 #!/sbin/bash var1=2 var2=3 let sum=var1+var2 echo $sum 使用let命令式,变量前不需要加$号 只用于整数运算,不适 ...

  4. APUE学习笔记——6.10 时间与时间例程 time_t

           Unix提供的最基本的时间服务室日历时间(纪元时间),也就是计算1970年1月1日0时0分0秒到当前的秒数.该秒数用time_t表示. typedef long time_t; /* 时 ...

  5. Linux驱动入门——构建和运行模块

    Hello world模块 本文介绍如何向内核中添加一个hello模块.该模块的功能是在模块加载时,向系统日志输出"hello world\n" 在模块卸载时输出"Goo ...

  6. New Concept English there (8)

    31w/m 56% The Great St Bernard Pass connects Switzerland to Italy. At 247o metres, it is the highest ...

  7. Linux文件在系统中传输

    一.文件的传输 1.命令:scp scp file user@ip:/dir ##把当前系统目录下的文件file复制到另一个系统目录下 scp user@ip:/file dir 2.命令:rsync ...

  8. pg_bulkload使用记录

    很久之前就使用过pg_bulkload来导入数据了,并做了对比试验,现在另一个项目又需要用了,这里做个记录: 1.rpm包比较老,下下来之后发现只支持到pg94,目前我用的是pg10,因此放弃. 2. ...

  9. java android 读写西门子PLC数据,包含S7协议和Fetch/Write协议,s7支持200smart,300PLC,1200PLC,1500PLC

    本文将使用一个gitHub开源的组件技术来读写西门子plc数据,使用的是基于以太网的TCP/IP实现,不需要额外的组件,读取操作只要放到后台线程就不会卡死线程,本组件支持超级方便的高性能读写操作 gi ...

  10. 框架重构:测试中的DateTime.Now

    存在的问题 DateTime.Now是C#语言中获取计算机的当前时间的代码: 但是,在对使用了DateTime.Now的方法进行测试时,由于计算机时间的实时性,期望值一直在变化.如:计算年龄. pub ...