PAT甲级——A1102 Invert a Binary Tree
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
先找到根节点,数字r未出现,则r为根节点,因为根节点不是任何节点的子节点
然后静态构造树
再调用平常的层序遍历和中序遍历
所谓的二叉树反转,就是原来先读左子树,再读右子树
现在改为先读右子树,再读左子树
#include <iostream>
#include <vector>
#include <queue>
using namespace std;
struct Node
{
int l, r;
};
int N, root[] = { };
Node tree[];
vector<int>lev, in;
void levelOrde(int t)
{
if (t == -)
return;
queue<int>q;
q.push(t);
while (!q.empty())
{
t = q.front();
q.pop();
lev.push_back(t);
if (tree[t].r != -)//先进右
q.push(tree[t].r);
if (tree[t].l != -)
q.push(tree[t].l);
}
}
void inOrder(int t)
{
if (t == -)
return;
inOrder(tree[t].r);
in.push_back(t);
inOrder(tree[t].l);
}
int main()
{
cin >> N;
char l, r;
for (int i = ; i < N; ++i)
{
cin >> l >> r;
if (l != '-')
{
tree[i].l = l - '';
root[l - ''] = -;//去除为根的可能性
}
else
tree[i].l = -;
if (r != '-')
{
tree[i].r = r - '';
root[r - ''] = -;//去除为根的可能性
}
else
tree[i].r = -;
}
for (int i = ; i < N; ++i)
{
if (root[i] == )
{
r = i;
break;//找到了根节点
}
}
levelOrde(r);
inOrder(r);
for (int i = ; i < N; ++i)
cout << lev[i] << (i == N - ? "" : " ");
cout << endl;
for (int i = ; i < N; ++i)
cout << in[i] << (i == N - ? "" : " ");
return ;
}
PAT甲级——A1102 Invert a Binary Tree的更多相关文章
- PAT甲级——1102 Invert a Binary Tree (层序遍历+中序遍历)
本文同步发布在CSDN:https://blog.csdn.net/weixin_44385565/article/details/90577042 1102 Invert a Binary Tree ...
- 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 Advanced 1102 Invert a Binary Tree (25) [树的遍历]
题目 The following is from Max Howell @twitter: Google: 90% of our engineers use the sofware you wrote ...
- 【PAT甲级】1110 Complete Binary Tree (25分)
题意: 输入一个正整数N(<=20),代表结点个数(0~N-1),接着输入N行每行包括每个结点的左右子结点,'-'表示无该子结点,输出是否是一颗完全二叉树,是的话输出最后一个子结点否则输出根节点 ...
- PAT_A1102#Invert a Binary Tree
Source: PAT A1102 Invert a Binary Tree (25 分) Description: The following is from Max Howell @twitter ...
- 1102 Invert a Binary Tree——PAT甲级真题
1102 Invert a Binary Tree The following is from Max Howell @twitter: Google: 90% of our engineers us ...
- PAT甲级:1064 Complete Binary Search Tree (30分)
PAT甲级:1064 Complete Binary Search Tree (30分) 题干 A Binary Search Tree (BST) is recursively defined as ...
- PAT 1102 Invert a Binary Tree[比较简单]
1102 Invert a Binary Tree(25 分) The following is from Max Howell @twitter: Google: 90% of our engine ...
随机推荐
- C9 vs 三星
我还是更喜欢 C9, 可惜当年的牛B人物 LemonNation 不在了,C9 赢 三星 一局的机会都没有了. 伟大的 LemonNation ,软件工程学硕士, 2014年,LemonNation ...
- Linux网络配置 RPM命令 samba服务 Linux目录结构
第一种方法: (1)用root身份登录,运行setup命令进入到 text mode setup utiliy对网络进行配置,这里可以进行ip,子网掩码,默认网关,dns的设置.(2)这时网卡的配置没 ...
- 整理及优化CSS代码的7个原则
作为网页设计师(前端工程师),你可能还记得曾经的那个网页大小建议:一个网页(包括HTML.CSS.Javacript.Flash和图片)尽量不要超过30KB的大小,随着互联网的日益庞大,网络带宽也在飞 ...
- C puts,gets
1. char *gets(char *s); 从输入缓冲区中读取一个字符串存储到字符指针变量 str 所指向的内存空间 注意事项:空间不检查合理:要使用功能必须准备足够大的空间: #include ...
- H5点击拨打电话,发短信
phone(){ window.location.href = 'tel:123123'// 拨打电话 window.location.href = 'sms:123123'// 发短信 }
- Windows copy
将一份或多份文件复制到另一个位置. COPY [/D] [/V] [/N] [/Y | /-Y] [/Z] [/L] [/A | /B ] source [/A | /B] [+ source ...
- python使用PIL处理图片后返回给前端的坑
一.python代码 这里有个坑,之前没有将bytes图片数据转成base64就返回到前端了,但在前端处理的时候,怎么都显示不出图片来,虽然数据拿到了,但bytes被传到前后变str了,所以怎么搞都没 ...
- System.Math.cs
ylbtech-System.Math.cs 1. 程序集 mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c56193 ...
- 有道云笔记新功能发现——有道云笔记剪报,完美解决不开会员保存csdn博客到本地的问题。
怎么用 方法一:谷歌插件 方法二:http://note.youdao.com/web-clipper-chrome.html 添加到书签 功能: 能够把网页浏览的内容保存到有道云笔记 解决了自己的难 ...
- day 81 Vue学习二之vue结合项目简单使用、this指向问题
Vue学习二之vue结合项目简单使用.this指向问题 本节目录 一 阶段性项目流程梳理 二 vue切换图片 三 vue中使用ajax 四 vue实现音乐播放器 五 vue的计算属性和监听器 六 ...