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

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

代码:

#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. ss-libev 源码解析local篇(4): server_recv_cb之STAGE_STREAM

    继续探索server_recv_cb,我们已经来到了STAGE_STREAM状态.如果在0.05秒的timer来之前客户端就有数据过来,server_recv_cb被调用,此时已经在stream状态就 ...

  2. python删除list中元素的三种方法

    a.pop(index):删除列表a中index处的值,并且返回这个值. del(a[index]):删除列表a中index处的值,无返回值. del中的index可以是切片,所以可以实现批量删除. ...

  3. 利用层序遍历(含空节点)和中序遍历重建二叉树 python

    给定一颗二叉树的层序遍历(不含None的形式)和中序遍历序列,利用两个序列完成对二叉树的重建. 还是通过一个例子来说明整个过程,下图所示的二叉树,层序遍历结果为[a,b,c,d,e],中序遍历结果为[ ...

  4. Ext JS 5 关于Store load返回json错误信息或异常的处理

    关于在store load的时候服务器返回错误信息或服务器出错的处理.ExtJS4应该也能用,没测试过. 这里有两种情况: 服务器返回错误json错误信息,状态为200 服务器异常,状态为500 一. ...

  5. PPP of DDD

    我是真够懒的了

  6. MySQL基础(《MySQL必知必会》简单总结)

    使用MySQL # 选择数据库 USE database_name; # 显示数据库 SHOW DATABASES; # 显示当前数据库的表 SHOW TABLES; # 显示特定表有哪些列 SHOW ...

  7. es6模块化开发

    export导出 import导入   export {a:b} Export default {a:b}

  8. LARAVEL 路由原理分析

    <?php class App {    protected $routes = [];    protected $responseStatus = '200 OK';    protecte ...

  9. 类中的迭代器__iter__

    什么是迭代器 有iter()函数返回,可以通过next(it)函数取值的对象就是迭代器 迭代器协议 迭代器协议是指对象能够使用next函数获取下一项数据,在没有下一项数据时触发一个StopIterat ...

  10. scrapy与scrapyd安装

    Scrapy是用python编写的爬虫程序. Scrapyd是一个部署与运行scrapy爬虫的应用,提供JSON API的调用方式来部署与控制爬虫 . 本文验证在fedora与centos是安装成功. ...