PAT_A1102#Invert a Binary Tree
Source:
Description:
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 (≤) 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
Keys:
Code:
/*
time: 2019-06-30 14:09:56
problem: PAT_A1102#Invert a Binary Tree
AC: 23:00 题目大意:
打印镜像树层序和中序遍历
输入:
第一行给出,结点数N<=10
接下来N行,结点i(0~n-1)的左孩子和右孩子 基本思路:
构造静态树遍历
*/
#include<cstdio>
#include<queue>
#include<string>
#include<iostream>
using namespace std;
const int M=1e2;
int mp[M]={},n;
struct node
{
int lchild,rchild;
}tree[M]; void LayerOrder(int root)
{
queue<int> q;
q.push(root);
int pt=;
while(!q.empty())
{
root = q.front();
q.pop();
printf("%d%c", root, ++pt==n?'\n':' ');
if(tree[root].rchild != -)
q.push(tree[root].rchild);
if(tree[root].lchild != -)
q.push(tree[root].lchild);
}
} void InOrder(int root)
{
if(root == -)
return;
static int pt=;
InOrder(tree[root].rchild);
printf("%d%c", root, ++pt==n?'\n':' ');
InOrder(tree[root].lchild);
} int main()
{
#ifdef ONLINE_JUDGE
#else
freopen("Test.txt", "r", stdin);
#endif // ONLINE_JUDGE scanf("%d", &n);
string r,l;
for(int i=; i<n; i++){
cin >> l >> r;
if(l == "-")
tree[i].lchild = -;
else{
tree[i].lchild = atoi(l.c_str());
mp[tree[i].lchild]=;
}
if(r == "-")
tree[i].rchild = -;
else{
tree[i].rchild = atoi(r.c_str());
mp[tree[i].rchild]=;
}
}
int root;
for(int i=; i<n; i++)
if(mp[i]==)
root=i;
LayerOrder(root);
InOrder(root); return ;
}
PAT_A1102#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 ( ...
- Invert a binary tree 翻转一棵二叉树
Invert a binary tree 翻转一棵二叉树 假设有如下一棵二叉树: 4 / \ 2 7 / \ / \ 1 3 6 9翻转后: 4 / \ 7 ...
- PAT1102: Invert a Binary Tree
1102. Invert a Binary Tree (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue ...
- 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
The following is from Max Howell @twitter: Google: 90% of our engineers use the software you wrote ( ...
随机推荐
- 用 Windows Live Writer 和 SyntaxHighlighter 插件写高亮代码
博客园内置支持SyntaxHighlighter代码着色,代码着色语法:<pre class='brush:编程语言'>代码</pre>. 需要注意的是:如何你使用Syntax ...
- ubuntu终端仿真程序和文件管理程序
1.SecureCRT是一款支持SSH(SSH1和SSH2)的终端仿真程序,简单的说是Windows下登录UNIX或Linux服务器主机的软件.可以理解为ubuntu下的Terminal. 如果Sec ...
- MVC 中对返回的 data 进行压缩
在webAPI 中返回数据,在数据量比较大的情况的下,返回的data 也可能比较大,有时候可能大于1兆,因此对数据进行压缩能极大的提高数据下载到客户端的时间,提高页面的加载速度. 思路: 在web a ...
- 获取客户端IP地址-----以及--------线上开启redis扩展
/** * 获取客户端IP地址 * @param integer $type 返回类型 0 返回IP地址 1 返回IPV4地址数字 * @return mixed */ function get_cl ...
- elasticsearch中TermQuery查不到数据问题
在java rest client中调用elasticsearch中的数据,精准匹配的termQuery查不到数据,这个问题是java rest client客户端自带的bug,换用matchPhra ...
- 前端(八)—— 高级布局:文档流、浮动布局、流式布局、定位布局、flex布局、响应布局
高级布局:文档流.浮动布局.流式布局.定位布局.flex布局.响应布局 一.文档流 1.什么是文档流 将窗体自上而下分成一行一行,块级元素从上至下.行内元素在每行中从左至右的顺序依次排放元素 2.本质 ...
- Java目录事件
当文件系统中的对象被修改时,我们可以监听watch服务以获取警报.java.nio.file包中的以下类和接口提供watch服务. Watchable接口 WatchService接口 WatchKe ...
- 条件sql ibatis
<!-- 多条件查询 --><select id="MS-CUSTOM-PANDECT-INFO-BY-CONDITIONS" resultMap="R ...
- 设置单网卡双ip
环境如下: [root@localhost ~]# cat /etc/redhat-releaseCentOS Linux release 7.7.1908 (Core) 安装时选择的最小化安装 -- ...
- 移动端新建html页面
这是一些头部设置 <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> < ...