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

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

代码:

#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. LeetCode OJ:Copy List with Random Pointer(复制存在随机链接的链表)

    A linked list is given such that each node contains an additional random pointer which could point t ...

  2. NI FPGA板卡程序设计概述

    NI公司提到了三种不同应用开发环境ADE:http://www.ni.com/white-paper/5956/zhs/ LabVIEW是NI公司主推的ADE,采用G语言(图像化语言),支持力度最大 ...

  3. powershell -enc参数无法解码base64编码payload的解决方案

    powershell的-enc参数允许传入一个base64编码过的powershell脚本字符串作为参数来执行该powershell脚本,该方法常被用于绕过杀毒软件的主动防御机制. 今天下午在做一个后 ...

  4. This is very likely to create a memory leak. Stack trace of thread

    1.错误描述 警告: The web application [cmp] appears to have started a thread named [Abandoned connection cl ...

  5. 当创建SDL工程发现“无法解析的外部符号 _SDL_main”出错

    当你创建一个新控制台程序,想调用SDL时,编译时发现如下出错: 1>------ 已启动生成: 项目: caipal, 配置: Debug Win32 ------1>  caipal.c ...

  6. MPAndroidChart Wiki(译文)~Part 4

    16. 动画 注意:本章的动画效果只会在API 11(Android3.0.x)及以上的Android版本上生效 在低于上述的Android版本中,动画将不会被执行,并不会导致程序崩溃. 所有类型的图 ...

  7. 05-python中函数的使用

    函数:就是让程序模块化,把具有独立功能的代码块当成一个整体封装成一个函数 首先打印一个佛主看看: print(" _ooOoo_ ") print(" o8888888o ...

  8. python3.4 使用BeautifulSoup问题

    事情 记得昨儿还是什么时候,反正是以前,肯定安装过BeautifulSoup,只不过当初可能用的是python setup.py install,这是Python2的安装.然而用Python3运行Be ...

  9. 【集成学习】lightgbm使用案例

    github地址 #!/usr/bin/env python2 # -*- coding: utf-8 -*- """ Created on Sat Mar 31 21: ...

  10. CocoaPods(pod install一直不动)

    CocoaPods安装和使用教程 如何在Mac 终端升级ruby版本 RubyGems 镜像 cocoapods无法使用(mac os 10.11升级导致pod: command not found)