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

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

代码:

#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:LRU Cache(最近使用缓存)

    Design and implement a data structure for Least Recently Used (LRU) cache. It should support the fol ...

  2. is7.0中发布mvc网站,一直无法正常执行路由的解决办法

    在config中加一句话: <system.webServer> <validation validateIntegratedModeConfiguration="fals ...

  3. .net 获取客户端Ip地址

    Request.ServerVariables["REMOTE_ADDR"]来取得客户端的IP地址,但如果客户端是使用代理服务器来访问,那取到的就是代理服务器的IP地址,而不是真正 ...

  4. vue 问题集合 |

    vue做类似选项卡                                                 点击改变curIndex ,  选项内容显示用 v-show="$inde ...

  5. spring之httpclient doget请求

    /**     * @param url        请求地址     * @param jsonString 加密后的字符串     * @return     * @throws ClientP ...

  6. VS2010 快捷键 (空格显示 绿点, Tab 显示箭头)

    转自http://www.cnblogs.com/xiaoyusmile/archive/2012/06/27/2566049.html VS2010 有用的快捷键 : Ctrl + r, ctrl ...

  7. 修改程序版本工具(ResHacker)使用说明20140902

    有时候我们需要自己修改dll版本号,那么ResHacker工具可以帮我们在不需要开发帮助下可以自己修改版本号: 工具:直接复制出来即可 1.双击打开工具 2.将dll文件或者exe文件拖拽进来,或者[ ...

  8. 重学CPP

    LINK : fatal error LNK1123: failure during conversion to COFF: file invalid or corrupt After install ...

  9. hexo部署Github博客

    例子:https://aquarius1993.github.io/blog/ 仓库:https://github.com/Aquarius1993/blog (前提是已经安装Xcode和git) 1 ...

  10. 现在很火的数据科学到底是什么?你对做DATA SCIENTIST感兴趣吗?

    转自– Warald (Email: iamxiaoning@gmail.com) 博客: http://www.1point3acres.com,微博:http://www.weibo.com/wa ...