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. Android NDK开发method GetStringUTFChars’could not be resolved

    Android NDK开发method GetStringUTFChars'could not be resolved 图1 最近用到android的ndk,但在eclipse中提示method Ge ...

  2. 【Java编程】Java学习笔记<二>

    种访问权限,而类的访问控制级别只有public和缺省的,当为public时,可以被任何包的其他类访问,当为缺省时,只能被同一包的其他类访问.如果类自身对另一个类是不可见的,即使类的成员声明为publi ...

  3. 重新初始化VS2010

    开始->所有程序->Microsoft Visual Studio 2010->Visual Studio Tools->Visual Stdio命令提示(2010)  这时会 ...

  4. Bookmarkable Pages

      Build a Bookmarkable Edit Page with JDeveloper 11g Purpose In this tutorial, you use Oracle JDevel ...

  5. "《算法导论》之‘树’":AVL树

    本文关于AVL树的介绍引自博文AVL树(二)之 C++的实现,与二叉查找树相同的部分则不作介绍直接引用:代码实现是在本文的基础上自己实现且继承自上一篇博文二叉查找树. 1.AVL树的介绍 AVL树是高 ...

  6. android微信登录,分享

    这几天开发要用到微信授权的功能,所以就研究了一下.可是微信开放平台接入指南里有几个地方写的不清不楚.在此总结一下,以便需要的人. 很多微信公众平台的应用如果移植到app上的话就需要微信授权登陆了. 目 ...

  7. Linux基础正则表达式字符汇整(characters)

    RE 字符 意义与范例 ^word 意义:待搜寻的字串(word)在行首! 范例:搜寻行首为 # 开始的那一行,并列出行号 grep -n '^#' regular_express.txt word$ ...

  8. kettle 的表输出 table output

    kettle的表输出: 双击后,看设置, 1,在connecttion后面,点击new里新建一个.设定各个选项值,如选择mysql类型,则配置hostname,database name,端口, 用户 ...

  9. HBase写被block的分析

    一个线上集群出现莫名奇妙不能写入数据的bug,log中不断打印如下信息: 引用 2011-11-09 07:35:45,911 INFO org.apache.hadoop.hbase.regions ...

  10. MongoDB下载安装测试及使用

    1.下载安装 64位:mongodb-win32-x86_64-enterprise-windows-64-2.6.4-signed.msi 余数为1的 db.collection.find({ &q ...