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

  1. #include <iostream>
  2. #include <algorithm>
  3. #include <cstring>
  4. #include <string.h>
  5. #include <cstdio>
  6. #include <queue>
  7.  
  8. using namespace std;
  9. const int maxn=;
  10. bool first=true;
  11. struct Node{
  12. int id;
  13. int left;
  14. int right;
  15. }node[maxn];
  16. int vis[maxn]; //没出现的即是根节点
  17. //层次遍历
  18. void level_order(int i){
  19. queue<Node>q;
  20. q.push(node[i]);
  21. Node tmp;
  22. while(!q.empty()){
  23. tmp=q.front();
  24. q.pop();
  25. if(first){
  26. first=false;
  27. printf("%d",tmp.id);
  28. }
  29. else
  30. printf(" %d",tmp.id);
  31. int l=tmp.left;
  32. int r=tmp.right;
  33. if(l!=-)
  34. q.push(node[l]);
  35. if(r!=-)
  36. q.push(node[r]);
  37. }
  38. }
  39. //中序遍历
  40. void in_order(int i){
  41. if(i==-)
  42. return;
  43. int l=node[i].left;
  44. int r=node[i].right;
  45. in_order(l);
  46. if(first){
  47. printf("%d",i);
  48. first=false;
  49. }
  50. else{
  51. printf(" %d",i);
  52. }
  53. in_order(r);
  54. }
  55. int main()
  56. {
  57. int n;
  58. char str1[],str2[];
  59. memset(vis,,sizeof(vis));
  60. scanf("%d",&n);
  61. for(int i=;i<n;i++){
  62. scanf("%s %s",str1,str2);
  63. node[i].id=i;
  64. if(str1[]=='-')
  65. node[i].right=-;
  66. else{
  67. node[i].right=str1[]-'';
  68. vis[str1[]-'']=;
  69. }
  70. if(str2[]=='-')
  71. node[i].left=-;
  72. else{
  73. node[i].left=str2[]-'';
  74. vis[str2[]-'']=;
  75. }
  76. }
  77. int root;
  78. for(int i=;i<n;i++){
  79. if(!vis[i]){
  80. root=i;
  81. break;
  82. }
  83. }
  84. first=true;
  85. level_order(root);
  86. printf("\n");
  87. first=true;
  88. in_order(root);
  89. return ;
  90. }

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. MSChart 设置饼图颜色 图例背景色 图例显示位置

    chartField.Series.Clear();            chartField.ChartAreas.Clear();            chartField.Legends.C ...

  2. PowerShell “execution of scripts is disabled on this system.”

    Set-ExecutionPolicy RemoteSigned

  3. [HZNOI #koishi] Magic

    [HZNOI #514] Magic 题意 给定一个 \(n\) 个点 \(m\) 条边的有向图, 每个点有两个权值 \(a_i\) 和 \(b_i\), 可以以 \(b_i\) 的花费把第 \(i\ ...

  4. Scala学习之路 (九)Scala的上界和下届

    一.泛型 1.泛型的介绍 泛型用于指定方法或类可以接受任意类型参数,参数在实际使用时才被确定,泛型可以有效地增强程序的适用性,使用泛型可以使得类或方法具有更强的通用性.泛型的典型应用场景是集合及集合中 ...

  5. ethereum/EIPs-725

    https://github.com/ethereum/EIPs/blob/master/EIPS/eip-725.md eip title author discussions-to status ...

  6. C#控件中的KeyDown、KeyPress 与 KeyUp事件浅谈

    研究了一下KeyDown,KeyPress 和 KeyUp 的学问.让我们带着如下问题来说明: 1.这三个事件的顺序是怎么样的? 2.KeyDown 触发后,KeyUp是不是一定触发? 3.三个事件的 ...

  7. C#自定义控件开发

    自定义控件开发 一般而言,Visual Studio 2005中自带的几十种控件已经足够我们使用了,但是,在一些特殊的需求中,可能需要一些特殊的控件来与用户进行交互,这时,就需要我们自己开发新的.满足 ...

  8. Python2.7-SciPy

    SciPy函数库在NumPy库的基础上增加了众多的数学.科学以及工程计算中常用的库函数.例如线性代数.常微分方程数值求解.信号处理.图像处理.稀疏矩阵等等 1.最小二乘拟合 详细介绍:https:// ...

  9. day38

    今日内容: 1.认识数据库 2.修改默认密码 3.常用操作指令 1.认识数据库 什么是MYSQL? 是一个关系型数据库管理系统,基于socket编写的C/S架构的软件 什么是数据库? 数据:用于记录事 ...

  10. Jquery属性练习

    <!DOCTYPE html> <html lang="en" xmlns="http://www.w3.org/1999/xhtml"> ...