题意:

输入一个正整数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. window snmp

    https://blog.csdn.net/weixin_30367543/article/details/99923014 https://jingyan.baidu.com/article/e3c ...

  2. linux命令解压压缩rar文件的详细步骤

    参考文件:https://www.cnblogs.com/qinglin/p/9007939.html

  3. linux centos7环境下安装apache2.4+php5.6+mysql5.6 安装及踩坑集锦

    linux centos7环境下安装apache2.4+php5.6+mysql5.6 安装及踩坑集锦(一) 一.Linux下安装MySQL 1.下载 下载地址:http://dev.mysql.co ...

  4. python多项式拟合:np.polyfit 和 np.polyld

    python数据拟合主要可采用numpy库,库的安装可直接用pip install numpy等. 1. 原始数据:假如要拟合的数据yyy来自sin函数,np.sin import numpy as ...

  5. COS上传图片和显示图片

    写这篇文章之前,我也是刚刚实现COS上传和显示图片.我百度了好多相关文章,COS上传图片成功的文章不少,上传后显示图片的文章几乎没有.于是写一篇记录下. COS上传图片推荐链接:https://blo ...

  6. C语言sprintf函数的深入理解

    由于sprintf跟printf在用法上几乎一样,只是打印的目的地不同而已,前者打印到字符串中,后者则直接在命令行上输出.这也导致sprintf比printf有用得多.所以本文着重介绍sprintf, ...

  7. ASP.NET MVC 获取表单数据

    public class Person { public string Name{get;set;} public string Phone{get;set;} } view层 @model Mode ...

  8. Vue.js ---Hello---1

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...

  9. 题解【洛谷P1618】 三连击(升级版)

    设三个数分别为n1.n2.n3,因为三个数的比为A:B:C,取一份量i,使得A·i=x,B·i=y,C·i=z(·是*的意思). 所以我们的代码只需要枚举i,并以此判断n1.n2.n3是否为三位数且包 ...

  10. soundtouch change pitch matlab implementation

    function output = changePitch(input, pitchInSemitones) % one octave is 12 semitones octave = pitchIn ...