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. OSB开发常用资料

    成功搭建OSB环境并运行HelloWorld项目 http://www.beansoft.biz/?p=2066 Oracle Service Bus 11gR1开发环境安装文档 http://www ...

  2. C# Oracle数据库操作类实例详解

    本文所述为C#实现的Oracle数据库操作类,可执行超多常用的Oracle数据库操作,包含了基础数据库连接.关闭连接.输出记录集.执行Sql语句,返回带分页功能的dataset .取表里字段的类型和长 ...

  3. 【一天一道LeetCode】#8. String to Integer (atoi)

    一天一道LeetCode系列 (一)题目 Implement atoi to convert a string to an integer. Hint: Carefully consider all ...

  4. 安卓Eclipse开发者的福音

    我们知道,谷歌已经放弃对Eclipse(ADT)的维护更新了,现在官网上也找不到ADT的下载链接了,我们大多数同学仍在使用的ADT版本可能已经很老了,估计大多数的SDK版本只到4.4,而,在尝试升级以 ...

  5. #pragma comment(转)

    此文转自微软MSDN.注意这是在Windows上才有的,Linux上可没有. #pragma comment( comment-type [,"commentstring"] ) ...

  6. Android群英传笔记——第二章:Android开发工具新接触

    Android群英传笔记--第二章:Android开发工具新接触 其实这一章并没什么可讲的,前面的安装Android studio的我们可以直接跳过,如果有兴趣的,可以去看看Google主推-Andr ...

  7. 有关java的引用传递,直接操作对象本身。直接删除BE的value中某值

    HashSet<String> refRegions = BE.get(regionName);    HashSet<String> values = new HashSet ...

  8. 基于SVMLight的文本分类

    支持向量机(Support Vector Machine)是Cortes和Vapnik于1995年首先提出的,它在解决小样本 .非线性及高维模式识别 中表现出许多特有的优势,并能够推广应用到函数拟合等 ...

  9. linux中syscall调用号查看

    可以用locate查找: locate unistd_32 //或者 locate unistd_64 以下是本猫在ubuntu下返回的结果: /usr/src/linux-headers-3.16. ...

  10. JSP 分页显示数据 (Oracle)

    要实现分页,首先我们要做的就是如何来编写SQL语句,网上也有很多,大家可以搜一下.在这里,我们使用一种比较常用的方式来编写SQL语句.代码如下: ----分页显示 select * from (sel ...