PAT1102: 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 思路 1.题目要求左右颠倒二叉树,并按层次遍历和中序遍历输出。那么其实只要在构造树的时候交换下输入数据就可以直接构造出一颗颠倒后的树了。
2.输出的时候需要注意空格,对于两种遍历的输出只要特殊标识下第一次的输出就行了。 代码
#include<iostream>
#include<vector>
#include<queue>
using namespace std;
class Node
{
public:
int left;
int right;
int value;
}; vector<Node> btree(10); int createTree(const int& N)
{
vector<bool> roots(N,true);
for(int i = 0;i < N ;i++)
{
char l,r;
cin >> l >> r;
btree[i].value = i;
//invert left
if(l != '-')
{
btree[i].right = l - '0';
roots[l-'0'] = false;
}
else
btree[i].right = -1;
//invert right
if(r != '-')
{
btree[i].left = r - '0';
roots[r-'0'] = false;
}
else
btree[i].left = - 1;
}
int root = 0;
for(int i = 0;i < N;i++)
{
if(roots[i] == true)
{
root = i;
break;
}
}
return root;
} void bfs(int root)
{
queue<int> q;
q.push(root);
while(!q.empty())
{
int cur = q.front();
q.pop();
if(cur == root)
cout << cur;
else
cout << " " << cur;
if(btree[cur].left != - 1)
q.push(btree[cur].left);
if(btree[cur].right != -1)
q.push(btree[cur].right);
}
cout << endl;
} int firstput = 0;
void inorder(int root)
{
if(root == -1)
return;
if(btree[root].left != -1)
inorder(btree[root].left); if( firstput++ == 0)
cout << root;
else
cout << " " <<root; if(btree[root].right != -1)
inorder(btree[root].right);
} int main()
{
int N;
while(cin >> N)
{
int root = createTree(N);
bfs(root); inorder(root);
}
}
PAT1102: Invert a Binary Tree的更多相关文章
- PAT-1102(Invert a Binary Tree)+二叉树的镜像+层次遍历+中序遍历+已知树的结构构树
Invert a Binary Tree pat-1102 import java.util.Arrays; import java.util.Queue; import java.util.Scan ...
- 1102. Invert a Binary Tree (25)
The following is from Max Howell @twitter: Google: 90% of our engineers use the software you wrote ( ...
- Invert a binary tree 翻转一棵二叉树
Invert a binary tree 翻转一棵二叉树 假设有如下一棵二叉树: 4 / \ 2 7 / \ / \ 1 3 6 9翻转后: 4 / \ 7 ...
- PAT A1102 Invert a Binary Tree (25 分)——静态树,层序遍历,先序遍历,后序遍历
The following is from Max Howell @twitter: Google: 90% of our engineers use the software you wrote ( ...
- A1102. Invert a Binary Tree
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 分) The following is from Max Howell @twitter: Google: 90% of our engine ...
- 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
The following is from Max Howell @twitter: Google: 90% of our engineers use the software you wrote ( ...
- PAT_A1102#Invert a Binary Tree
Source: PAT A1102 Invert a Binary Tree (25 分) Description: The following is from Max Howell @twitter ...
随机推荐
- LeetCode之“链表”:在O(1)时间删除链表节点
下边讨论暂不包括尾节点. 一般来说,我们要删除链表中的一个节点是需要知道其上一节点的.但我们真的需要吗? 其实我们可以将待删节点的下一节点的值和指向的下一节点赋予待删节点,然后删除待删节点的下一节点. ...
- 添加启动游戏过渡场景Default Splash Scene(Unity3D开发之十三)
添加启动游戏过渡场景Default Splash Scene(Unity3D开发之十三) 猴子原创,欢迎转载.转载请注明: 转载自Cocos2Der-CSDN,谢谢! 原文地址: http://blo ...
- JavaScript单线程的疑问与解答
问: JavaScript是单线程的,有任务队列,比如使用setTimeou(func,secs)来在secs毫秒后向任务队列添加func.但是,setTimeout后面跟一个死循环,那么死循环导致任 ...
- ITU-T G.1080 IPTV的体验质量(QoE)要求 (Quality of experience requirements for IPTV services)
IPTV的服务质量(QoE)要求 Quality of experience requirements for IPTV services Summary This Recommendation de ...
- Linux - man page
使用man date来查看date命令的详细信息. lucifer@lucifer-virtual-machine:~$ man date DATE(1) User Commands DATE(1) ...
- Linux - 简单好用的计算器 bc
如果在文本模式当中,突然想要作一些简单的加减乘除,偏偏手边又没有计算器!这个时候要笔算吗? Linux有提供一支计算程序,那就是bc.你在命令列输入bc后,屏幕会显示出版本信息, 之后就进入到等待指示 ...
- “《编程珠玑》(第2版)第2章”:C题(查找变位词,排序)
C题是这样子的: 给定一个英语字典,找出其中的所有变位词集合.例如,“pots”.“stop”和“tops”互为变位词,因为每一个单词都可以通过改变其他单词中字母的顺序来得到. 下段分析摘自该书(P1 ...
- 用xml来编写动画
我们可以使用代码来编写所有的动画功能,这也是最常用的一种做法.不过,过去的补间动画除了使用代码编写之外也是可以使用XML编写的,因此属性动画也提供了这一功能,即通过XML来完成和代码一样的属性动画功能 ...
- BMP 转 YUV (BMP2YUV)
本文介绍BMP 转 YUV.其实这是以前"数据压缩"实验课上的内容,前几天有人问我相关的问题,突然发现自己有一段时间没有接触BMP也有些生疏了,因此翻出资料总结一下. BMP文件格 ...
- 粒子滤波(PF:Particle Filter)
先介绍概念:来自百科 粒子滤波指:通过寻找一组在状态空间中传播的随机样本来近似的表示概率密度函数,再用样本均值代替积分运算,进而获得系统状态的最小方差估计的过程,波动最小,这些样本被形象的称为&quo ...