题意:

输入一个正整数N(<=10),接着输入0~N-1每个结点的左右儿子结点,输出这颗二叉树的反转的层次遍历和中序遍历。

AAAAAccepted code:

 #define HAVE_STRUCT_TIMESPEC
#include<bits/stdc++.h>
using namespace std;
int a[][];
bool vis[];
void bfs(int r){
queue<int>q;
q.push(r);
cout<<r;
while(!q.empty()){
int now=q.front();
q.pop();
if(a[now][]!=-){
cout<<" ";
cout<<a[now][];
q.push(a[now][]);
}
if(a[now][]!=-){
cout<<" ";
cout<<a[now][];
q.push(a[now][]);
}
}
}
int flag=;
void dfs(int r){
if(a[r][]!=-)
dfs(a[r][]);
if(flag)
cout<<" ";
cout<<r;
flag=;
if(a[r][]!=-)
dfs(a[r][]);
}
int main(){
ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
for(int i=;i<;++i)
a[i][]=a[i][]=-;
int n;
cin>>n;
for(int i=;i<n;++i){
char x;
cin.ignore();
cin>>x;
cin.ignore();
if(x!='-')
a[i][]=x-'',vis[x-'']=;
char y;
cin>>y;
if(y!='-')
a[i][]=y-'',vis[y-'']=;
}
int root=;
for(int i=;i<n;++i)
if(!vis[i])
root=i;
bfs(root);
cout<<"\n";
dfs(root);
return ;
}

【PAT甲级】1102 Invert a Binary Tree (25 分)(层次遍历和中序遍历)的更多相关文章

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

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

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

  3. 【PAT甲级】1110 Complete Binary Tree (25分)

    题意: 输入一个正整数N(<=20),代表结点个数(0~N-1),接着输入N行每行包括每个结点的左右子结点,'-'表示无该子结点,输出是否是一颗完全二叉树,是的话输出最后一个子结点否则输出根节点 ...

  4. PAT甲级——A1102 Invert a Binary Tree

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

  5. 1102. Invert a Binary Tree (25)

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

  6. PAT 甲级 1066 Root of AVL Tree (25 分)(快速掌握平衡二叉树的旋转,内含代码和注解)***

    1066 Root of AVL Tree (25 分)   An AVL tree is a self-balancing binary search tree. In an AVL tree, t ...

  7. leetcode 题解:Binary Tree Inorder Traversal (二叉树的中序遍历)

    题目: Given a binary tree, return the inorder traversal of its nodes' values. For example:Given binary ...

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

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

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

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

随机推荐

  1. 转载:openmax基本概念

    https://yellowmax.blog.csdn.net/article/details/78080168 https://yellowmax.blog.csdn.net/article/det ...

  2. python 音频可视化

    代码整理好放在 github 上了: https://github.com/darkchii/visualize bilibili 演示视频:https://www.bilibili.com/vide ...

  3. vscode里的NPM脚本

    NPM脚本的开启与关闭 点击设置-功能-任务 控制为所有任务提供程序扩展启用"提供任务".如果"任务:运行任务"命令速度较慢,则禁用任务提供程序的自动检测可能会 ...

  4. yolo系列阅读笔记(v1-v3)

    yolov1 模型输出的概率建模 图片首先被分割为S*S的网格(grid cell).如果一个bbox的中心落在一个网格里,则该网格负责检测该物体.(对于pascal数据集,S定为7) 每个网格预测B ...

  5. centos添加用户并赋予 root管理员权限

     centos添加用户并赋予 root管理员权限 用centos时,root用户一般都是超级管理员使用的,一般不轻易给别人,但是有时候同事安装软件时需要root账号,又不得不给,只能重新建一个用户,并 ...

  6. 后台异常 - org/apache/oro/text/regex/MalformedPatternException

    解决办法 1.将JDK换成1.6.023或者以上的,如果不行进行下面的办法操作 2.缺少了jakarta-oro-2.0.8.jar文件,将此包放入工程中

  7. Hydra暴力破解工具

    hydra [[[-l LOGIN|-L FILE] [-p PASS|-P FILE]] | [-C FILE]] [-e nsr] [-o FILE] [-t TASKS] [-M FILE [- ...

  8. C++ vector的用法(转)

    原文链接:https://blog.csdn.net/qinyuehong/article/details/92837359

  9. windows下划分逻辑分区

    运行命令窗口后,我们输入命令"diskpart"回车! 然后我们在DISKPART>后面输入select disk 0 选择我们的硬盘,然后回车!当然如果你电脑上有好几块硬盘 ...

  10. VC++编译选项

    -优化- /O1 最小化空间 minimize space /Op[-] 改善浮点数一致性 improve floating-pt consistency /O2 最大化速度 maximize spe ...