1102 Invert a Binary Tree (25 分)
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
作者
CHEN, Yue
单位
浙江大学
代码长度限制
16 KB
时间限制
400 ms
内存限制
64 MB
//1102 Invert a Binary Tree (25 分)#include<bits/stdc++.h>
using namespace std;
int n;
const int MAX_N=22;
vector<int>tree[MAX_N];
bool havaFa[MAX_N];
void level(int root){vector<int>levelArr;queue<int>q;q.push(root);while(!q.empty()){int fr= q.front();q.pop();levelArr.emplace_back(fr);if(tree[fr].size()){for(int i=tree[fr].size()-1;i>=0;i--){int val=tree[fr][i];if(val!=-1){q.push(val);}}
// for(auto val: tree[fr]){// q.push(val);
// }}}int fir=1;for(auto val:levelArr){if(fir)fir=0;else printf(" ");printf("%d",val);}putchar('\n');
}
int inFir=1;
void in(int node){if(node==-1)return;
// if(tree[node].size()>=2)in(tree[node][1]);if(inFir)inFir=0;else printf(" ");printf("%d",node);// if(tree[node].size()>=1)in(tree[node][0]);
}
int main(){scanf("%d", &n);getchar();
// printf("n: %d\n",n);for(int i=0;i<n;i++){char u,v;scanf("%c %c",&u,&v);getchar();if(u!='-'){tree[i].emplace_back(u-'0');havaFa[u-'0']=1;}else{// 之前没有 写push -1, 样例2 错了
// 原因是 不写的话, 会把 - 1 和 1 - 当作同一种东西tree[i].emplace_back(-1);}if(v!='-'){tree[i].emplace_back(v-'0');havaFa[v-'0']=1;}else{tree[i].emplace_back(-1);}}int root=0;for(int nodeNum=0;nodeNum<n;nodeNum++){if(!havaFa[nodeNum]){root=nodeNum;break;}}level(root);in(root);}
1102 Invert a Binary Tree (25 分)相关推荐
- 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 (层序遍历+中序遍历)
本文同步发布在CSDN:https://blog.csdn.net/weixin_44385565/article/details/90577042 1102 Invert a Binary Tree ...
- 1102 Invert a Binary Tree(甲级)
1102 Invert a Binary Tree (25分) The following is from Max Howell @twitter: Google: 90% of our engine ...
- PAT甲级-二叉树的遍历-1102 Invert a Binary Tree解题思路
1102 Invert a Binary Tree (25 分) 思路 翻转二叉树 后序遍历翻转即可,由于给出每个结点的左右儿子,所以这里用到二叉树的静态写法更加方便 这里有个坑,bool数组初始化为 ...
- PAT甲级1102 Invert a Binary Tree:[C++题解]反转二叉树、递归
文章目录 题目分析 题目链接 题目分析 反转二叉树这道题目特别出名!!!,是因为Homebrew这款Mac上大火的软件的作者到google面试,就考了这道题.面试官说了下面这段话:你竟然连在白板上翻转 ...
- 1102 Invert a Binary Tree
题目来源:PAT (Advanced Level) Practice The following is from Max Howell @twitter: Google: 90% of our eng ...
- 1102 Invert a Binary Tree (25point(s))
1102 Invert a Binary Tree (25point(s)) The following is from Max Howell @twitter: Google: 90% of our ...
- 1110 Complete Binary Tree (25分)测试点2,3,4
Given a tree, you are supposed to tell if it is a complete binary tree. Input Specification: Each in ...
- 1110 Complete Binary Tree (25 分)【难度: 一般 / 知识点: 判断完全二叉树】
https://pintia.cn/problem-sets/994805342720868352/problems/994805359372255232 建树并给其赋值,如果是完全二叉树,那么max ...
最新文章
- 深度学习中的贝叶斯统计简介
- 刚刚,我国智能科学技术最高奖揭晓!
- 德勤元宇宙综观:愿景、技术和应对
- java 找序列最小值_【C++编程练习】任意给定 n 个有序整数,求这 n 个有序整数序列的最大值,中位数和最小值...
- MySQL基本语句和连接字符串
- Insurance 项目——Mybetis-generator生成
- MySQL 报错:Translating SQLException with SQL state '42000', error code '1064', message
- 发动机冒黑烟_发动机总冒黑烟 用这招解决最快!
- linux下好看的中文字体,推荐一款 Linux 上比较漂亮的字体(转)
- 计算机毕业设计项目推荐(源码+论文+PPT)
- 小白教你把数字翻译成字符串
- 利用c#快速知道哪些qq好友空间屏蔽了自己
- Linux C语言 pthread_cond_wait()、pthread_cond_timedwait()函数(不允许cond被唤醒时产生竞争,所以需要和互斥锁搭配)
- 基于SpringBoot HII健身房app
- Qt开发的国内外知名软件
- java讲座总结报告_20175313 张黎仙《Java综合讲座》第十三周课堂测试总结
- “十月围城”中国高校SAS数据分析大赛将再燃战火
- STM32学习记录:输入捕获应用
- java计算ip地址
- 6款渐变标签彩色flash按钮