C++学习之路 | PTA(甲级)—— 1099 Build A Binary Search Tree (30分)(带注释)(精简)

1099 Build A Binary Search Tree (30分)
A Binary Search Tree (BST) is recursively defined as a binary tree which has the following properties:
The left subtree of a node contains only nodes with keys less than the node’s key.
The right subtree of a node contains only nodes with keys greater than or equal to the node’s key.
Both the left and right subtrees must also be binary search trees.
Given the structure of a binary tree and a sequence of distinct integer keys, there is only one way to fill these keys into the tree so that the resulting tree satisfies the definition of a BST. You are supposed to output the level order traversal sequence of that tree. The sample is illustrated by Figure 1 and 2.
figBST.jpg
Input Specification:

Each input file contains one test case. For each case, the first line gives a positive integer N (≤100) which is the total number of nodes in the tree. The next N lines each contains the left and the right children of a node in the format left_index right_index, provided that the nodes are numbered from 0 to N−1, and 0 is always the root. If one child is missing, then −1 will represent the NULL child pointer. Finally N distinct integer keys are given in the last line.
Output Specification:

For each test case, print in one line the level order traversal sequence of that tree. All the numbers must be separated by a space, with no extra space at the end of the line.
Sample Input:

9
1 6
2 3
-1 -1
-1 4
5 -1
-1 -1
7 -1
-1 8
-1 -1
73 45 11 58 82 25 67 38 42
Sample Output:

58 25 82 11 38 67 45 73 42
关键思路:中序遍历这棵树得到的结点顺序应该是给出的数值序列从小到大的排列顺序

#include<iostream>
#include<algorithm>
#include<queue>
#include<vector>
using namespace std;
const int maxn = 101;
struct node {int id;//存储节点编号int data;//存储节点存储的数据int left;//左孩子int right;//右孩子
}no[maxn];
int n;
int k = 0;
vector<int>v(maxn);
void inorder(int root)
{if (root == -1)return;//当该节点为-1,returninorder(no[root].left);no[root].data = v[k++];//中序遍历的结果就是二叉搜索树的从小到大排序inorder(no[root].right);
}
int main()
{cin >> n;for (int i = 0; i < n; i++)//初始化{no[i].id = i;cin >> no[i].left;cin >> no[i].right;}for (int i = 0; i < n; i++)//存储需要插入的数据{cin >> v[i];}sort(v.begin(), v.begin() + n);//排序inorder(0);//中序遍历,0是根节点queue<int>q;int flag = 0;q.push(0);//根节点入队while (!q.empty()){int fa = q.front();q.pop();if (flag++ == 0)cout << no[fa].data;else cout << " " << no[fa].data;//输出格式if (no[fa].left != -1)q.push(no[fa].left);if (no[fa].right != -1)q.push(no[fa].right);}}

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

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

相关文章

python 命令行 解析模块 optparse、argparse

optparse&#xff1a;https://docs.python.org/zh-cn/3/library/optparse.htmlargparse &#xff1a;https://docs.python.org/zh-cn/3/library/argparse.html3.2 版后已移除 optparse 模块&#xff0c;并且将不再继续开发&#xff1b;开发转至 argparse 模块进行。 ​1、argpa…

构建插件式的应用程序框架(六)----通讯机制(ZT)

前天发了构建插件式的应用程序框架(五)&#xff0d;&#xff0d;&#xff0d;&#xff0d;管理插件这篇文章&#xff0c;有几个朋友在回复中希望了解插件之间是如何通讯的。这个系列的文章写到这里&#xff0c;也该谈谈这个问题了&#xff0c;毕竟已经有了插件管理。不知道大家…

人工智能产业2018年待解的三大难题

来源&#xff1a;人民邮电报概要&#xff1a;2017年&#xff0c;人工智能领域在算法、政策、资金等方面已经出现了三大突破&#xff0c;业界欢欣鼓舞的情形很像1999年年底网络泡沫泛滥时的情形。2017年&#xff0c;人工智能领域在算法、政策、资金等方面已经出现了三大突破&…

矩阵连乘问题(c++)

矩阵连乘问题 问题描述&#xff1a; 给定n个矩阵&#xff1a;A1,A2,…,An&#xff0c;其中Ai与Ai1是可乘的&#xff0c;i1&#xff0c;2…&#xff0c;n-1。确定计算矩阵连乘积的计算次序&#xff0c;使得依此次序计算矩阵连乘积需要的数乘次数最少。输入数据为矩阵个数和每个矩…

我在犹豫是不是该收集这几首MP3

我在犹豫是不是该收集这几首MP3&#xff0c;确实&#xff0c;曲子歌词都非常不错。可我找不到收藏的理由。总是一个想法&#xff0c;让我放弃和错过了很多东西&#xff1a;我已经错过和失去了很多&#xff0c;这一次又算什么呢&#xff1f;这样的想法让我在面对很多人或事都已经…

QuestMobile 2017年中国移动互联网年度报告

来源&#xff1a;QuestMobile2017年&#xff0c;科技的风口兜兜转转&#xff0c;从直播、VR到AI再到区块链、短视频泛娱乐IP&#xff0c;最终在2017年底定格在了知识付费上&#xff0c;然而这并没有结束&#xff0c;紧随知识付费而来的就是撒币、大撒币……这就是中国移动互联网…

Python 读写配置文件模块: configobj 和 configParser

参考&#xff1a;http://www.voidspace.org.uk/python/configobj.html Python模块之ConfigParser - 读写配置文件&#xff1a;http://www.cnblogs.com/victorwu/p/5762931.html Python 官网 configparser 文档&#xff1a;https://docs.python.org/3.7/library/configparser.…

快速排序(c++)

1、快速排序的思想 快速排序就是给基准数据找在数组中正确位置的过程&#xff0c;一旦基准位置的正确位置找到&#xff0c;那基准位置左右两边经过同样的步骤递归也可以有序&#xff0c;最终整体数组有序。 整体可以理解为三个步骤&#xff1a; 1、先从队尾开始向前扫描且当l …

设计模式之禅--思维导图

原图ProcessOn里搜索&#xff1a;设计模式之禅

有BRT,为啥还建公交港湾

原来快速公交和普通公交要一块儿跑历山路公交港湾示意图(制图&#xff1a;赵国陆&#xff09;   “历山路上既然跑快速公交车&#xff0c;有BRT站台&#xff0c;还要公交港湾干吗&#xff1f;”21日&#xff0c;本报报道了新公交港湾将在历山路亮相的消息后&#xff0c;不少市…

2018展望| AI:巨头生态开始站队,深入垂直行业才能赚钱

来源&#xff1a;36氪“AI改变世界”这件事&#xff0c;在2018年会更值得人期待。不只是BAT&#xff0c;京东在谈智能仓储配送&#xff0c;滴滴在谈智慧交通……BAT&#xff0c;以及滴滴、京东这样的小巨头&#xff0c;手中攥着大量数据、也有直接服务消费者的场景&#xff0c;…

记录遇到的Python陷阱和注意点

来源&#xff1a;http://www.cnblogs.com/wilber2013/p/5178620.html 最近使用Python的过程中遇到了一些坑&#xff0c;例如用datetime.datetime.now()这个可变对象作为函数的默认参数&#xff0c;模块循环依赖等等。 在此记录一下&#xff0c;方便以后查询和补充。 避免可变…

归并排序(c++)

归并排序 归并排序&#xff08;Merge Sort&#xff09;是建立在归并操作上的一种有效&#xff0c;稳定的排序算法&#xff0c;该算法是采用分治法&#xff08;Divide and Conquer&#xff09;的一个非常典型的应用。将已有序的子序列合并&#xff0c;得到完全有序的序列&#x…

js 操作 iframe

//页面内有两个iframe<table width"100%"> <tr width"100%"> <td width"100%"> <iframe id"iframeNum1" src"a.aspx" style"display:block;" width"100%"…

五大风口产业全景手绘图(新能源汽车、人工智能等)

来源&#xff1a;一览众车概要&#xff1a;五大风口产业全景手绘图&#xff08;新能源汽车、人工智能等&#xff09;一、新能源汽车二、人工智能三、住房租赁住房租赁产业蕴含着哪些发展机会&#xff1f;各参与方的竞争格局如何&#xff1f;未来人们租房会更便利吗&#xff1f;…

Python进阶之“属性(property)”详解

来源&#xff1a;http://python.jobbole.com/80955/ 参考&#xff1a;廖雪峰的官方网站 使用__slots__ 参考&#xff1a;廖雪峰的官方网站 使用 property Python中有一个被称为属性函数(property)的小概念&#xff0c;它可以做一些有用的事情。在这篇文章中&#xff0c;我们…

冒泡排序(c++)

冒泡排序&#xff08;Bubble Sort&#xff09; 是一种计算机科学领域的较简单的排序算法。 它重复地走访过要排序的元素列&#xff0c;依次比较两个相邻的元素&#xff0c;如果顺序&#xff08;如从大到小、首字母从Z到A&#xff09;错误就把他们交换过来。走访元素的工作是重复…

微软研发致胜策略读书笔记(转)

创建人&#xff1a;X公司程序员 yunshichen 这篇是我读《微软研发致胜策略》后整理的笔记。身为一个软件开发部门的主管&#xff0c;你的职责是什么&#xff1f;单单完成项目是不足够的&#xff0c;如果你的目标是这样&#xff0c;那么你会做错很多事。我认为准确的表述应该是…

原型模式详解

PrototypeClass.java public class PrototypeClass implements Cloneable {Overridepublic PrototypeClass clone() {try {final PrototypeClass instance (PrototypeClass) super.clone();return instance;} catch (CloneNotSupportedException e) {return null;}} } 1、构…

python 结巴分词(jieba)学习

来源&#xff1a;http://www.gowhich.com/blog/147?utm_sourcetuicool&utm_mediumreferral 源码下载的地址&#xff1a;https://github.com/fxsjy/jieba 演示地址&#xff1a;http://jiebademo.ap01.aws.af.cm/ 特点 1&#xff0c;支持三种分词模式&#xff1a; a,精确模式…