PAT A1102 Invert a Binary Tree

Sample Input:

8
1 -
- -
0 -
2 7
- -
- -
5 -
4 6

Sample Output:

3 7 2 6 4 0 5 1
6 5 7 4 3 2 0 1
word meaning
Max Howell 马克斯·豪威尔
indices (index的复数)
  • 思路 1:使用静态二叉树(含父节点)
  1. findRoot:从0节点开始,不断向上遍历到父节点为空(f == -1)
  2. 从root开始,进行逆层序遍历,逆中序遍历(交换遍历左右子树的顺序即可)
  3. 层序遍历,直接输出每次出队的元素,中序遍历是递归,不好控制输出,故用一个数组存储结果,遍历后再格式化输出
  • code 1:
#include <stdio.h>
#include <iostream>
#include <vector>
#include <queue>
using namespace std;
const int maxn = 110;
int n, root;
char tmpl, tmpr;
struct Node{int data, lc, rc, f;Node(){f = lc = rc = -1;}
}node[maxn];
int findRoot(){int idex = 0;while(node[idex].f != -1){idex = node[idex].f;}return idex;
}
void revLevelOrder(int root){queue<int> q;q.push(root);int cnt = 0;while(!q.empty()){int now = q.front();cnt++;printf("%d", node[now].data);if(cnt != n) printf(" ");q.pop();if(node[now].rc != -1) q.push(node[now].rc); if(node[now].lc != -1) q.push(node[now].lc);}printf("\n");
}
vector<int> in;
void revInOrder(int root){if(node[root].rc != -1) revInOrder(node[root].rc);in.push_back(root);if(node[root].lc != -1) revInOrder(node[root].lc);
}
void outputIn(){for(int i = 0; i < n; ++i){printf("%d", in[i]);if(i < n-1) printf(" ");}
}
int main(){scanf("%d", &n);for(int i = 0; i < n; ++i){getchar();    //!!!:Wrong 1: scanf读入char,要吸收掉上一轮结束时的\n scanf("%c %c", &tmpl, &tmpr);if(tmpl == '-') node[i].lc = -1;else{node[tmpl-'0'].f = i;node[i].lc = tmpl - '0';} if(tmpr == '-') node[i].rc = -1;else{node[tmpr-'0'].f = i;node[i].rc = tmpr - '0'; }node[i].data = i;}root = findRoot();revLevelOrder(root);revInOrder(root);outputIn();return 0;
}
  • 不需要output控制inOrder的输出:
vector<int> in;
int cnt1 = 0;
void revInOrder(int root){if(root == -1) return;if(node[root].rc != -1) revInOrder(node[root].rc);printf("%d", root);cnt1++;if(cnt1 < n) printf(" ");if(node[root].lc != -1) revInOrder(node[root].lc);
}
  • TIPS: 遍历之前要判断结点是否为空

  • 思路 2:
    在输入时,就交换左右孩子,然后按正常的层序遍历和中序遍历输出

  • code 2:

#include <stdio.h>
#include <iostream>
#include <vector>
#include <queue>
using namespace std;
const int maxn = 110;
int n, root;
char tmpl, tmpr;
struct Node{int lc, rc; //!!! TIPS 1:不需要data,下标就是idex
}node[maxn];
bool has[maxn]; //找root
int cnt = 0;
void print(int root){//单独用一个print()控制输出格式 printf("%d", root);cnt++;if(cnt < n) printf(" ");else printf("\n");
}
int change(char c){return c == '-' ? -1 : c-'0';
}
int findRoot(){for(int i = 0; i < n; ++i){if(has[i] == false)return i;}
}
void levelOrder(int root){queue<int> q;q.push(root);while(!q.empty()){int now = q.front();print(now);q.pop();if(node[now].lc != -1) q.push(node[now].lc);if(node[now].rc != -1) q.push(node[now].rc); }cnt = 0;
}
void inOrder(int root){if(root == -1) return;if(node[root].lc != -1) inOrder(node[root].lc);print(root);if(node[root].rc != -1) inOrder(node[root].rc);
}
int main(){scanf("%d", &n);for(int i = 0; i < n; ++i){scanf("%*c%c %c", &tmpl, &tmpr);    //!!!:TIPS 2:"%*c"可以在scanf()中吸收一个字符 int lchild = change(tmpl), rchild = change(tmpr);//!!!输入时交换左右孩子 node[i].rc = lchild;if(lchild != -1) has[lchild] = true;node[i].lc = rchild; if(rchild != -1) has[rchild] = true;}root = findRoot();levelOrder(root);inOrder(root);return 0;
}
  • TIPS 2: "%*c" 可以在scanf()中吸收一个字符

  • 思路 3:
    先用后序遍历交换左右孩子,再层序和中序遍历

  • code 3:

void postOrder(int root){if(root == -1) return;if(node[root].lc != -1) postOrder(node[root].lc);if(node[root].rc != -1) postOrder(node[root].rc);swap(node[root].lc, node[root].rc);
}
  • T2 code:
#include <bits/stdc++.h>
using namespace std;
const int maxn = 15;
struct Node{int lc, rc;
}node[maxn];
bool HasFather[256];
int FindRoot(int n){for(int i = 0; i < n; ++i){if(HasFather[i + '0'] == false) return i;}
}
//bool inq[maxn];
void RevLevel(int root, int n){int cnt = 0;queue<int> q;q.push(root);
//  inq[root] = true;  //层序遍历不需要inq[] while(!q.empty()){int now = q.front();printf("%d", now);if(++cnt < n) printf(" ");q.pop();if(node[now].rc != -1) q.push(node[now].rc);if(node[now].lc != -1) q.push(node[now].lc);}printf("\n");
}
int cntIn = 0;
void RevIn(int root, int n){if(root == -1) return;RevIn(node[root].rc, n);    printf("%d", root);if(++cntIn < n) printf(" ");RevIn(node[root].lc, n);
}int main(){int n;scanf("%d", &n);for(int i = 0; i < n; ++i){char tl, tr;getchar();scanf("%c %c", &tl, &tr);node[i].lc = tl != '-' ? tl - '0' : -1;node[i].rc = tr != '-' ? tr - '0' : -1;HasFather[tl] = true;HasFather[tr] = true;}int root = FindRoot(n);RevLevel(root, n);RevIn(root, n);return 0;
}
  • T3 code: 数组存储二叉树,先逆中序遍历,将中序序列存入一个vector,同时用id控制将节点存入一个数组,在遍历数组即得到逆层序序列
#include <bits/stdc++.h>
using namespace std;
const int maxn = 110;
int seq[maxn];
struct Node
{int data, lc, rc;
}node[maxn];
int Change(char s[])
{return s[0] == '-' ? -1 : atoi(s);
}
vector<int> in;
void RevInorder(int id, int root)
{if(root == -1) return;RevInorder(id * 2, node[root].rc);in.push_back(root);seq[id] = root;RevInorder(id * 2 + 1, node[root].lc);
}
int main()
{int n;scanf("%d", &n);int r = (n - 1) * n / 2 - (n + 1);for(int i = 0; i < n; ++i){char lc[5], rc[5];scanf("%s %s", lc, rc);node[i].lc = Change(lc);node[i].rc = Change(rc);r -= (node[i].lc + node[i].rc);}memset(seq, -1, sizeof(seq));RevInorder(1, r);printf("%d", seq[1]);for(int i = 2; i < maxn; ++i){if(seq[i] != -1) printf(" %d", seq[i]);}printf("\n");for(int i = 0; i < n; ++i){printf("%d", in[i]);if(i < n-1) printf(" ");}return 0;
}

PAT A1102 Invert a Binary Tree相关推荐

  1. PAT甲级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:[C++题解]反转二叉树、递归

    文章目录 题目分析 题目链接 题目分析 反转二叉树这道题目特别出名!!!,是因为Homebrew这款Mac上大火的软件的作者到google面试,就考了这道题.面试官说了下面这段话:你竟然连在白板上翻转 ...

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

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

  4. 1102. Invert a Binary Tree (25)-PAT甲级真题

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

  5. PAT甲级-二叉树的遍历-1102 Invert a Binary Tree解题思路

    1102 Invert a Binary Tree (25 分) 思路 翻转二叉树 后序遍历翻转即可,由于给出每个结点的左右儿子,所以这里用到二叉树的静态写法更加方便 这里有个坑,bool数组初始化为 ...

  6. 1102 Invert a Binary Tree

    题目来源:PAT (Advanced Level) Practice The following is from Max Howell @twitter: Google: 90% of our eng ...

  7. PAT甲级1110 Complete Binary Tree:[C++题解]判断完全二叉树

    文章目录 题目分析 题目链接 题目分析 分析: 按照层序的顺序将完全二叉树存在下标从1开始的数组中.如果是完全二叉树,会将数组中1 ~ n这些位置填满,最大下标就是n,如果最大下标大于n,说明中间有空 ...

  8. 1102 Invert a Binary Tree(甲级)

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

  9. Invert a Binary Tree

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

最新文章

  1. C#命名规范 C#控件的缩写大全
  2. Markov Decision Processes
  3. Normal-Inverse Gamma Mixture简介
  4. postman发送json格式的post请求(亲测)
  5. Java多线程编程笔记4:Java内存模型
  6. 深入浅析HTML5中的article和section的区别
  7. pythonplotline_带误差条的python绘图线,pythonplotlinewitherrorbar
  8. laravel mysql 视图_视图入门:Laravel 支持的视图格式以及在路由中的基本使用
  9. 物联网-移远M26模块OpenCPU开发第2讲(FLASH处理)
  10. 2018 ACM-ICPC World Finals - Beijing F.Go with the Flow
  11. k8s使用glusterfs实现动态持久化存储
  12. 【渝粤教育】国家开放大学2018年秋季 1117t机电控制与可编程序控制 参考试题
  13. 外贸单证管理系统如何解决企业制单问题
  14. P1069 细胞分裂
  15. Hack The Box——Blunder
  16. 监控摄像机镜头角度和距离计算表
  17. 论文研究结论怎么写?
  18. 用java设计一个矩形类_6-1 设计一个矩形类Rectangle (10分)
  19. Keep your Eyes on the Lane: Real-time Attention-guided Lane Detection
  20. 奈奎斯特与香农定理_奈奎斯特定理和香农定理有什么区别?

热门文章

  1. 人类记忆系统之谜,也许就是这么回事儿
  2. mysql主从延迟过高
  3. java 入门教程推荐
  4. 调用mstsc命令_远程桌面命令是什么 如何使用命令连接远程桌面
  5. 牛顿法(Newton Methods)、阻尼牛顿法和拟牛顿法
  6. Pycharm安装教程以及Pycharm汉化教程,超级详细
  7. Saiku + Kylin 多维分析平台探索
  8. 数据分析实战(一) Pandas分析Kaggle电子游戏销量数据集
  9. 基于MATLAB的汽车出入库计时计费车牌识别系统
  10. 树莓派CM4_TBOX扩展板(针对车机和工业应用)之移远EC20 4G模块的操作演示