就是把输入给的左孩子右孩子互换一下,然后输出层次遍历和中序遍历。

#include <iostream>
#include <algorithm>
#include <cstring>
#include <string.h>
#include <cstdio>
#include <queue> using namespace std;
const int maxn=;
bool first=true;
struct Node{
int id;
int left;
int right;
}node[maxn];
int vis[maxn]; //没出现的即是根节点
//层次遍历
void level_order(int i){
queue<Node>q;
q.push(node[i]);
Node tmp;
while(!q.empty()){
tmp=q.front();
q.pop();
if(first){
first=false;
printf("%d",tmp.id);
}
else
printf(" %d",tmp.id);
int l=tmp.left;
int r=tmp.right;
if(l!=-)
q.push(node[l]);
if(r!=-)
q.push(node[r]);
}
}
//中序遍历
void in_order(int i){
if(i==-)
return;
int l=node[i].left;
int r=node[i].right;
in_order(l);
if(first){
printf("%d",i);
first=false;
}
else{
printf(" %d",i);
}
in_order(r);
}
int main()
{
int n;
char str1[],str2[];
memset(vis,,sizeof(vis));
scanf("%d",&n);
for(int i=;i<n;i++){
scanf("%s %s",str1,str2);
node[i].id=i;
if(str1[]=='-')
node[i].right=-;
else{
node[i].right=str1[]-'';
vis[str1[]-'']=;
}
if(str2[]=='-')
node[i].left=-;
else{
node[i].left=str2[]-'';
vis[str2[]-'']=;
}
}
int root;
for(int i=;i<n;i++){
if(!vis[i]){
root=i;
break;
}
}
first=true;
level_order(root);
printf("\n");
first=true;
in_order(root);
return ;
}

PAT甲题题解-1102. Invert a Binary Tree (25)-(建树,水题)的更多相关文章

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

  2. 1102. Invert a Binary Tree (25)

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

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

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

  4. 【PAT甲级】1102 Invert a Binary Tree (25 分)(层次遍历和中序遍历)

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

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

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

  6. 1102 Invert a Binary Tree——PAT甲级真题

    1102 Invert a Binary Tree The following is from Max Howell @twitter: Google: 90% of our engineers us ...

  7. PAT 1102 Invert a Binary Tree[比较简单]

    1102 Invert a Binary Tree(25 分) The following is from Max Howell @twitter: Google: 90% of our engine ...

  8. PAT 1102 Invert a Binary Tree

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

  9. PAT甲题题解-1124. Raffle for Weibo Followers-模拟,水题

    水题一个,贴个代码吧. #include <iostream> #include <cstdio> #include <algorithm> #include &l ...

随机推荐

  1. MySQL基础之 排序与限制,聚合

    排序与限制 ORDER BY 作用:取出按照某个字段进行排序后的记录结果集. 配合:常与DESC  和ASC一块使用:默认是ASC,表示升序.DESC表示降序 LIMIT 作用:用于显示数据的一部分记 ...

  2. WaitForMultipleObjects

    WaitForMultipleObjects是Windows中的一个功能非常强大的函数,几乎可以等待Windows中的所有的内核对象 函数原型为: DWORD WaitForMultipleObjec ...

  3. 修改win下ras/pppoe/l2tp等连接数限制

    最近宽带重新拔号一直不换ip,拔了十几分钟IP仍然不变,想起几年前的pppoe多拔,多拔几个PPPOE占着老IP,再拔新的IP出来用 注册表路径,子项中查看MatchingDeviceId判断协议类型 ...

  4. CSS 居中大全(转)

    引用:http://jinlong.github.io/blog/2013/08/13/centering-all-the-directions/ CSS 居中大全 AUG 13TH, 2013 |  ...

  5. 团队作业——Alpha冲刺 4/12

    团队作业--Alpha冲刺 冲刺任务安排 杨光海天 今日任务:着手进行编辑界面的布局,插入控件,并进行参数调整. 明日任务:继续完善编辑界面控件,学习控件交互功能. 郭剑南 今日任务:上网查阅学习了关 ...

  6. 利用gulp 插件gulp.spritesmith 完成小图合成精灵图,并自动输出样式文件

    安装依赖 yarn add gulp yarn add gulp.spritesmith 本次依赖的版本是: "gulp": "^3.9.1" "gu ...

  7. 如何弹出QQ临时对话框实现不添加好友在线交谈效果

    如何不添加好友弹出QQ临时对话框实现在线交谈效果,这样的一个需求,我们真的是太需要了,实现起来也很简单,一行代码即可搞定,需要的朋友可以参考下 其实这个很简单,在img我们加入一个a标签,然后 < ...

  8. 【转】网络安全——一图看懂HTTPS建立过程

    阅读目录 准备工作(对应图中prepare1234) 发起链接 最后 关于网络安全加密的介绍可以看之前文章: 1. 网络安全——数据的加密与签名,RSA介绍2. Base64编码.MD5.SHA1-S ...

  9. centos限制远程尝试密码次数

    CentOS中有一个pam_tally2.so的PAM模块,来限定用户的登录失败次数,如果次数达到设置的阈值,则锁定用户.编译PAM的配置文件 # vim /etc/pam.d/login添加: au ...

  10. Chrome安装metamask

    到chrome网上应用商店去下载metamask: 然后查找metamask,进行安装即可 因为我这已经安装好了,所以按钮处是评分,否则应该是安装按钮 安装好后查看chrome://extension ...