1102 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 (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;建树完成后可以使用先序遍历或者是后序遍历进行树的反转,改变原来树的结构,随后就是简单的遍历操作啦。

#include<iostream>
#include<string>
#include<vector>
#include<queue>
using namespace std;
const int maxn = 100;
struct node {//二叉树静态写法int lchild=-1, rchild=-1;
}no[maxn];
int n;
int in_flag = 0, le_flag = 0;//在中序遍历和层序遍历时控制格式;
void postorder(int root)//后序遍历进行反转
{if (root==-1){return;}postorder(no[root].lchild);postorder(no[root].rchild);swap(no[root].lchild, no[root].rchild);//交换左右孩子,反转;
}
void inorder(int root)//中序遍历二叉树
{if (root == -1){return;}inorder(no[root].lchild);if (in_flag++ == 0)cout << root;else cout << " " <<root;inorder(no[root].rchild);
}
void level(int root)//层序遍历二叉树
{queue<int>q;q.push(root);int le_flag = 0;while (!q.empty()){int tmp = q.front();q.pop();if (le_flag++ == 0)cout << tmp;else cout << " " << tmp;if (no[tmp].lchild != -1)q.push(no[tmp].lchild);if (no[tmp].rchild != -1)q.push(no[tmp].rchild);}}
int main()
{int fa[11]{ 0 };//记录每个节点是否有父节点cin >> n;for (int i = 0; i < n; i++)//建立静态二叉树{string str1, str2;cin >> str1 >> str2;if (str1 != "-"){no[i].lchild = stoi(str1);fa[no[i].lchild] = 1;}if (str2 != "-"){no[i].rchild = stoi(str2);fa[no[i].rchild] = 1;}}int i = 0;for (i; i < n; i++)//寻找根节点,根节点的父节点不存在{if (fa[i] == 0){break;}}postorder(i);level(i);cout << endl;inorder(i);
}

也可以不对树进行改变,因为题目只是需要正确的输出就行,我们就可以只模拟输出。

#include<iostream>
#include<string>
#include<vector>
#include<queue>
using namespace std;
const int maxn = 100;
struct node {//二叉树静态写法int lchild=-1, rchild=-1;
}no[maxn];
int n;
int in_flag = 0, le_flag = 0;//在中序遍历和层序遍历时控制格式;
void inorder(int root)//不进行节点交换,先遍历右子树
{if (root==-1){return;}inorder(no[root].rchild);if (in_flag++ == 0)cout << root;else cout << " " << root;inorder(no[root].lchild);
}
void level(int root)//层序遍历二叉树
{queue<int>q;q.push(root);int le_flag = 0;while (!q.empty()){int tmp = q.front();q.pop();if (le_flag++ == 0)cout << tmp;else cout << " " << tmp;if (no[tmp].rchild != -1)q.push(no[tmp].rchild);//从右往左pushif (no[tmp].lchild != -1)q.push(no[tmp].lchild);//}}
int main()
{int fa[11]{ 0 };//记录每个节点是否有父节点cin >> n;for (int i = 0; i < n; i++)//建立静态二叉树{string str1, str2;cin >> str1 >> str2;if (str1 != "-"){no[i].lchild = stoi(str1);fa[no[i].lchild] = 1;}if (str2 != "-"){no[i].rchild = stoi(str2);fa[no[i].rchild] = 1;}}int i = 0;for (i; i < n; i++)//寻找根节点,根节点的父节点不存在{if (fa[i] == 0){break;}}level(i);cout << endl;inorder(i);}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/497057.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

从寻找可敬的人类开始,扩展未来人类生存的8个维度

来源&#xff1a;资本实验室作者&#xff1a;李鑫从小村庄到大城市&#xff0c;从国内到国外&#xff0c;从地球到月球&#xff0c;从太阳系到银河系……什么样的距离才是最远的距离&#xff1f;从地球的内部&#xff0c;到每一个原子&#xff0c;再到我们的情绪&#xff0c;哪…

开源 Python网络爬虫框架 Scrapy

开源 Python 网络爬虫框架 Scrapy&#xff1a;http://blog.csdn.net/zbyufei/article/details/7554322 介绍 所谓网络爬虫&#xff0c;就是一个在网上到处或定向抓取数据的程序&#xff0c;当然&#xff0c;这种说法不够专业&#xff0c;更专业的描述就是&#xff0c;抓取特定网…

SQL2005结合ROW_NUMBER()高效分页存储过程

SQL2005结合ROW_NUMBER()高效分页存储过程&#xff1a;CREATE PROCEDURE [dbo].[sp_Accounts_GetUserListPaged] PageIndex INT, PageSize INT AS BEGIN WITH UserList AS ( SELECT ROW_NUMBER() OVER (ORDER BY RegDate DESC)AS Row, * FROM Accounts_Users) SELECT …

微服务架构设计模式~为应用程序定义微服务架构

为应用程序定义微服务架构 第一步&#xff1a;定义系统操作 第二步&#xff1a;定义服务 第三步&#xff1a;定义服务API和协作方式 第一步&#xff1a;定义系统操作 第二步&#xff1a;定义服务 第三步&#xff1a;定义服务API和协作方式

1079 Total Sales of Supply Chain(甲级)

1079 Total Sales of Supply Chain (25分) A supply chain is a network of retailers&#xff08;零售商&#xff09;, distributors&#xff08;经销商&#xff09;, and suppliers&#xff08;供应商&#xff09;-- everyone involved in moving a product from supplier to…

用 Python 爬虫框架 Scrapy 爬取心目中的女神

From &#xff1a;http://www.cnblogs.com/wanghzh/p/5824181.html 本博文将带领你从入门到精通爬虫框架 Scrapy&#xff0c;最终具备爬取任何网页的数据的能力。 本文以校花网为例进行爬取&#xff0c;校花网&#xff1a;http://www.xiaohuar.com 让你体验爬取校花的成就感。 …

深入Atlas系列:综合示例(1) - 调用服务器端方法时直接获得客户端具体类型...

摘要: 在使用ASP.NET AJAX时&#xff0c;大家对于返回服务器端的复杂类型的情况经常会遇到问题。Dflying兄写了一篇文章来说明在如何在客户端得到Sys.Preview.Data.DataTable对象的文章&#xff0c;但是这种方法需要在客户端进行Sys.Preview.Data.DataTable的构造&#xff0c;那…

1090 Highest Price in Supply Chain(甲级)

1090 Highest Price in Supply Chain (25分) A supply chain is a network of retailers&#xff08;零售商&#xff09;, distributors&#xff08;经销商&#xff09;, and suppliers&#xff08;供应商&#xff09;-- everyone involved in moving a product from supplier …

微服务架构设计模式~识别系统操作

第一步&#xff1a;创建由关键类组成的抽象领域模型&#xff0c;这些关键类提供用于描述系统操作的词汇表&#xff1b; 第二步&#xff1a;确定系统操作&#xff0c;并根据领域模型描述每个系统操作的行为 领域模型主要源自用户故事中提及的名词&#xff0c;系统操作主要来自用…

Facebook、微软、谷歌三大研究巨头齐聚首,共同探讨人工智能发展现状和趋势

作者&#xff1a; 思颖、李诗概要&#xff1a;日前 AAAS 在 reddit 上组织了一场问答&#xff0c;Facebook 人工智能研究院 Yann LeCun&#xff0c;微软研究院院长 Eric Horvitz&#xff0c;谷歌研究总监 Peter Norvig 共同出席此次活动&#xff0c;回答了观众提出的一系列问题…

《大话设计模式》Python 版代码实现

From&#xff1a;http://www.cnblogs.com/wuyuegb2312/archive/2013/04/09/3008320.html 一、简单工厂模式 模式特点&#xff1a;工厂根据条件产生不同功能的类。 程序实例&#xff1a;四则运算计算器&#xff0c;根据用户的输入产生相应的运算类&#xff0c;用这个运算类处理具…

LeCun亲授的深度学习入门课:从飞行器的发明到卷积神经网络

Root 编译整理量子位 出品 | 公众号 QbitAI深度学习和人脑有什么关系&#xff1f;计算机是如何识别各种物体的&#xff1f;我们怎样构建人工大脑&#xff1f;这是深度学习入门者绕不过的几个问题。很幸运&#xff0c;这里有位大牛很乐意为你讲解。2月6日&#xff0c;UCLA&#…

微服务架构设计模式~根据业务能力进行服务拆分

业务能力定义了一个组织的工作 组织的业务能力通常是指这个组织的业务是做什么&#xff0c;它们通常是稳定的。 与之相反&#xff0c;组织采用何种方式来实现它的业务能力&#xff0c;是随着时间不断变化的。 识别业务能力 一个组织有哪些业务能力&#xff0c;是通过对组织的…

微服务架构设计模式~根据子域进行服务拆分

子域 领域驱动为每个子域定义单独的领域模型。子域是领域的一部分&#xff0c;领域是DDD中用来描述应用程序问题域的一个术语。识别子域的方式跟识别业务能力一样&#xff1a;分析业务并识别业务的不同专业领域&#xff0c;分析产出的子域定义结果也会跟业务能力非常接近。 限…

高通:全球NB-IoT/eMTC最新现状

来源&#xff1a;5G概要&#xff1a;全球NB-IoT/eMTC最新现状行业观察未来智能实验室是人工智能学家与科学院相关机构联合成立的人工智能&#xff0c;互联网和脑科学交叉研究机构。由互联网进化论作者&#xff0c;计算机博士刘锋与中国科学院虚拟经济与数据科学研究中心石勇、刘…

微服务架构设计模式~交互方式

一对一一对多同步模式请求/响应无异步模式 异步请求/响应 单向通知 发布/订阅 发布/异步响应 一对一的交互方式 1、请求/响应&#xff1a; 一个客户端向服务端发起请求&#xff0c;等待响应&#xff1b;客户端期望服务端很快就会发送响应。在一个基于线程的应用中&#xff0c…

1106 Lowest Price in Supply Chain(甲级)

1106 Lowest Price in Supply Chain (25分) A supply chain is a network of retailers&#xff08;零售商&#xff09;, distributors&#xff08;经销商&#xff09;, and suppliers&#xff08;供应商&#xff09;-- everyone involved in moving a product from supplier t…

2018年看好这些半导体企业

来源&#xff1a;钜亨网对半导体产业来说&#xff0c;去年是一个大年&#xff0c;无论哪个领域&#xff0c;都挣得盘满钵满。进入了2018&#xff0c;半导体产业将会面临哪些新状况&#xff1f;让我们来盘点一下&#xff01;DRAM今年供需稳定记忆体厂商持续获利的好年DRAM价格走…

AI黑箱:我们要用AI解释AI?

来源&#xff1a;亿欧概要&#xff1a;AI算法对人类生活的影响越来越大&#xff0c;但它们内部的运作往往是不透明的&#xff0c;人们对这种技术的工作方式也愈加感到担忧。AI算法对人类生活的影响越来越大&#xff0c;但它们内部的运作往往是不透明的&#xff0c;人们对这种技…

1053 Path of Equal Weigh(甲级)

1053 Path of Equal Weight (30分) Given a non-empty tree with root R, and with weight W ​i ​​ assigned to each tree node T ​i ​​ . The weight of a path from R to L is defined to be the sum of the weights of all the nodes along the path from R to any l…