1102. Invert a Binary Tree (25)

时间限制
400 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue

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的更多相关文章

  1. PAT-1102(Invert a Binary Tree)+二叉树的镜像+层次遍历+中序遍历+已知树的结构构树

    Invert a Binary Tree pat-1102 import java.util.Arrays; import java.util.Queue; import java.util.Scan ...

  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. Invert a binary tree 翻转一棵二叉树

    Invert a binary tree 翻转一棵二叉树 假设有如下一棵二叉树: 4  / \   2    7  / \   / \ 1  3 6  9翻转后: 4     /    \    7 ...

  4. PAT A1102 Invert a Binary Tree (25 分)——静态树,层序遍历,先序遍历,后序遍历

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

  5. A1102. Invert a Binary Tree

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

  6. PAT 1102 Invert a Binary Tree[比较简单]

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

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

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

  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_A1102#Invert a Binary Tree

    Source: PAT A1102 Invert a Binary Tree (25 分) Description: The following is from Max Howell @twitter ...

随机推荐

  1. 【Qt编程】设计ColorBar颜色栏

    画过图的都知道,我们常常用颜色的深浅来表示值的大小,在Matlab作图中,我们使用的是colorbar这个函数来给出颜色的直观参考.下面给出Matlab的示例:在Matlab命令窗口输入: figur ...

  2. Linux Platform Device and Driver

    从 Linux 2.6 起引入了一套新的驱动管理和注册机制 :Platform_device 和 Platform_driver . Linux 中大部分的设备驱动,都可以使用这套机制 , 设备用 P ...

  3. 图像检索:FCTH(Fuzzy Color and Texture Histogram)算法

    模糊颜色和纹理直方图(Fuzzy Color and Texture Histogram,FCTH) 本文节选自论文<基于半监督和主动学习相结合的图像的检索研究> FCTH 特征可从 3 ...

  4. ANDROID窗口管理服务实现机制和架构分析

     一.功能 窗口管理是ANDROID框架一个重要部分,主要包括如下功能: )Z-ordered的维护 )窗口的创建.销毁 )窗口的绘制.布局 )Token管理,AppToken )活动窗口管理(F ...

  5. 一个操作cvs格式的c++类

    经常需要使用excel,或者把有的数据用excel打开,程序可以生成cvs格式的文件,这样就可以excel打开并处理了,于是找了一个处理cvs的c++类跟大家分享 代码出处找不到了: 代码如下: St ...

  6. LeetCode之“数学”:Plus One

    题目链接 题目要求: Given a non-negative number represented as an array of digits, plus one to the number. Th ...

  7. android 高斯模糊实现

    高斯模糊 高斯模糊就是将指定像素变换为其与周边像素加权平均后的值,权重就是高斯分布函数计算出来的值. 一种实现 点击打开链接<-这里是一片关于高斯模糊算法的介绍,我们需要首先根据高斯分布函数计算 ...

  8. LeetCode之旅(15)-Odd Even Linked List

    题目描述: Given a singly linked list, group all odd nodes together followed by the even nodes. Please no ...

  9. C# 压缩PDF图片

    文档中包含图片的话,会使得整个文档比较大,占用存储空间且不利于快速.高效的传输文件.针对一些包含大量高质图片的PDF文档,若是对图片进行压缩,可以有效减少文档的占用空间.并且,在文档传输过程中也可以减 ...

  10. Java + Selenium + TestNG + Maven

    环境准备: 1. Java: Install Java jdk: Version: java 1.8 or aboveConfigure Java Environment Variables:Add ...