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. Java常见运算符整理

    本文是在学习中的总结,欢迎转载但请注明出处:http://blog.csdn.net/pistolove/article/details/44724267 本文主要介绍Java中常见的运算符,重点介绍 ...

  2. 认识Zygote

    概述 在java中不同的虚拟机实例会为不同的应用分配不同内存,为了使Android系统尽快启动,使用了Zygote来预加载核心类库和一些加载时间长的类(超过1250ms),让Dalvik虚拟机共享代码 ...

  3. 如何在linux上构建objective-c程序

    swfit目前还是os x独占,以后会不会扩展到其他系统还未可知,但objective-c并不只存在于os x,在linux下gcc和clang都支持obj-c哦,下面简单把如何在ubuntu上构建o ...

  4. solr研磨之facet

    作者:战斗民族就是干 转载请注明地址:http://www.cnblogs.com/prayers/p/8822417.html Facet 开门见山,facet解决的就是筛选,我是把它理解为一种聚合 ...

  5. Javascript、CSS、HTML面试题

    1 JS中的三种弹出式消息提醒(警告窗口.确认窗口.信息输入窗口)的命令是什么? alert     confirm     prompt 2声明一个已经存在一个CSS有几种方式? 1.导入一个已经存 ...

  6. iframe中 父页面和子页面查找元素的方法

    从父页面中查找iframe子页面中对象的方法:JS: document.getElementById('iframe').contentWindow //查找iframe加载的页面的window对象 ...

  7. 视频客观质量评价工具:MSU Video Quality Measurement Tool【ssim,psnr】

    MSU Video Quality Measurement Tool(msu vqmt)是莫斯科国立大学(Moscow State University)的Graphics and Media Lab ...

  8. SQLServer2PostgreSQL迁移过程中的几个问题

    1.PostgreSQL 跨平台迁移工具Migration Toolkit的使用指南:http://www.enterprisedb.com/docs/en/8.4/mtkguide/Table%20 ...

  9. C#将一个枚举里面所有描述和value绑定到下拉列表的方法

    /// <summary> /// 获取枚举值的描述,如果没有描述,则返回枚举名称 /// </summary> /// <param name="en&quo ...

  10. Ocelot中文文档-中间件注入和重写

    警告!请谨慎使用. 如果您在中间件管道中看到任何异常或奇怪的行为,并且正在使用以下任何一种行为.删除它们,然后重试! 当在Startup.cs中配置Ocelot的时候,可以添加或覆盖中间件.如下所示: ...