PAT甲级Invert a Binary Tree 柳神层序遍历的思路值得借鉴
1102 Invert a Binary Tree (25分)
The following is from Max Howell @twitter:
Google: 90% of our engineers use the software you wrote (Homebrew), but you can’t invert a binary tree on a whiteboard so fuck off.
Now it’s your turn to prove that YOU CAN invert a binary tree!
Input Specification:
Each input file contains one test case. For each case, the first line gives a positive integer N (≤10) which is the total number of nodes in the tree – and hence the nodes are numbered from 0 to N−1. Then N lines follow, each corresponds to a node from 0 to N−1, and gives the indices of the left and right children of the node. If the child does not exist, a - will be put at the position. Any pair of children are separated by a space.
Output Specification:
For each test case, print in the first line the level-order, and then in the second line the in-order traversal sequences of the inverted tree. There must be exactly one space between any adjacent numbers, and no extra space at the end of the line.
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
思路:就注意是翻转的二叉树,反转就是输入的左孩子,保存到右孩子的位置,就行了,然后层序 中序遍历就好啦。
#include <iostream>
#include <string>
using namespace std;
#include<vector>
#include<queue>struct node
{int num;node *rchild,*lchild;node():rchild(NULL),lchild(NULL) {}//最初时候左右孩子节点指针都是NULL
};vector<int> out2;
vector<int> out1;void inOrder(node *root)//中序遍历
{if(root==NULL)return;inOrder(root->lchild);//cout<<root->num;out2.push_back(root->num);inOrder(root->rchild);
}int main()
{int N;cin>>N;int info[N][2];for(int i=0; i<N; i++)//记录保存结点左右孩子的信息{char a,b;cin>>a>>b;if(a!='-'){info[i][0]=a-'0';}elseinfo[i][0]=-1;if(b!='-'){info[i][1]=b-'0';}elseinfo[i][1]=-1;}int sign[N]= {0}; //这样才能全部赋值为0 ,里面填1 就是sign[0]是1 其他为0,求那个事根节点node sn[N];//从新开辟了空间g++ 编译器 可支持这种写法,要不然就直接生成999个结点的数组也行for(int i=0; i<N; i++)//赋值每个孩子结点的左右指针,并顺便判断谁是根节点,没有父节点的就是了{sn[i].num=i;if(info[i][1]!=-1)//1存储的右孩子信息,但是使用lchild 左孩子指针指向,这就是翻转了{sn[i].lchild=&sn[info[i][1]];sign[info[i][1]]=-1;}if(info[i][0]!=-1){sn[i].rchild=&sn[info[i][0]];sign[info[i][0]]=-1;}}int root;for(int i=0; i<N; i++)if(sign[i]==0)root=i;inOrder(&sn[root]);//下面是实现层序遍历vector<int> out1;queue<node*> que;que.push(&sn[root]);while(que.size()!=0){queue<node*> get;while(!que.empty()){out1.push_back(que.front()->num);if(que.front()->lchild!=NULL)get.push(que.front()->lchild);if(que.front()->rchild!=NULL)get.push(que.front()->rchild);que.pop();}que=get;}//层序输出for(int i=0; i<out1.size(); i++){if(i==0)cout<<out1[i];elsecout<<' '<<out1[i];}//中序输出cout<<endl;for(int i=0; i<out2.size(); i++){if(i==0)cout<<out2[i];elsecout<<' '<<out2[i];}return 0;
}
我也看了看柳神的代码,感觉还是差距啊,50行搞定,层序遍历实现也很巧妙
PAT甲级Invert a Binary Tree 柳神层序遍历的思路值得借鉴相关推荐
- PAT甲级1110 Complete Binary Tree:[C++题解]判断完全二叉树
文章目录 题目分析 题目链接 题目分析 分析: 按照层序的顺序将完全二叉树存在下标从1开始的数组中.如果是完全二叉树,会将数组中1 ~ n这些位置填满,最大下标就是n,如果最大下标大于n,说明中间有空 ...
- PAT A1102 Invert a Binary Tree
PAT A1102 Invert a Binary Tree Sample Input: 8 1 - - - 0 - 2 7 - - - - 5 - 4 6 Sample Output: 3 7 2 ...
- PAT甲级——1102 Invert a Binary Tree (层序遍历+中序遍历)
本文同步发布在CSDN:https://blog.csdn.net/weixin_44385565/article/details/90577042 1102 Invert a Binary Tree ...
- PAT甲级1102 Invert a Binary Tree:[C++题解]反转二叉树、递归
文章目录 题目分析 题目链接 题目分析 反转二叉树这道题目特别出名!!!,是因为Homebrew这款Mac上大火的软件的作者到google面试,就考了这道题.面试官说了下面这段话:你竟然连在白板上翻转 ...
- 1102. Invert a Binary Tree (25)-PAT甲级真题
The following is from Max Howell @twitter: Google: 90% of our engineers use the software you wrote ( ...
- PAT甲级-二叉树的遍历-1102 Invert a Binary Tree解题思路
1102 Invert a Binary Tree (25 分) 思路 翻转二叉树 后序遍历翻转即可,由于给出每个结点的左右儿子,所以这里用到二叉树的静态写法更加方便 这里有个坑,bool数组初始化为 ...
- PAT甲级1064 Complete Binary Search Tree (30分):[C++题解]完全二叉搜索树BST
文章目录 题目分析 题目链接 题目分析 思路: 第一步,构造含有n个结点的完全二叉树:第二步,将n个数值填入,使其满足二叉搜索树的性质. 对于第一步: 完全二叉树用一维数组可以存下,不过从根结点的下标 ...
- 1102 Invert a Binary Tree(甲级)
1102 Invert a Binary Tree (25分) The following is from Max Howell @twitter: Google: 90% of our engine ...
- 1102 Invert a Binary Tree
题目来源:PAT (Advanced Level) Practice The following is from Max Howell @twitter: Google: 90% of our eng ...
最新文章
- ant PageHeaderWrapper 返回上一页
- Java中? extends T和? super T的理解
- ESXI使用记录---安装vSphere(VCSA)
- 3.1.7 封装的应用
- 下载大文件 不经过php 直接让apache发送文件给客户端 mod_xsendfile
- 13 款 JavaScript 模板引擎
- 物理机存放mysql实例原则_MySQL优化笔记(四)--表的设计与优化(单表、多表)...
- SwipeRefreshLayout官方推荐下拉刷新
- 从uptime、stress、mpstat、pidstat观察CPU密集型、IO密集型、进程密集型切换的系统性能
- JSP访问数据库,Session对象和九大内置对象
- jquery easyui 多选下拉框的实现
- 朗读评价语言集锦_表扬朗读好的评语简短
- MFCC里面的log energy和matlab实现
- 25_删除分类(一对多的删除)
- 怎样看开源代码版权_版权声明在开源代码中泛滥成灾
- 怎么看公司邮箱服务器地址,怎么看企业邮箱是哪里的
- Bailian-1的个数
- Android反编译工具与反编译步骤及常见问题
- 软件开发之计划阶段: ”声控打鼓”游戏的”用户/场景”分析
- php求一个人的生日,【星月随笔】一个人的 生日
热门文章
- 基于Keras的卷积神经网络模型预测--狗的品种识别
- 学业竞技实业网址窗口
- 由LG 的G2手机浅析国产旗舰机的方向
- 手打css 问号提示-用于输入框,标题后面...
- NavicatPremium写的MySQL文件去哪?
- 蛋白质相互作用系列:GN算法
- 软件著作权申请材料及申请流程?
- SAP中采购收货控制中的配置问题分析
- NAS 详细搭建方案 - 安装NAS操作系统
- 关于向数据库中插入数据时报“An explicit value for the identity column in table can only be specified when ...“的错误