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);}}

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

  1. PAT甲级1099 Build A Binary Search Tree (30分):[C++题解]建立二叉搜索树、dfs和bfs

    文章目录 题目分析 题目链接 题目分析 题意重述:给定一棵二叉树的结构,和待填的数值,请将数据填到二叉树中的结点中,使之满足二叉搜索树的性质. 然后按照层序遍历输出数值. 分析: 本题分两步. 第一步 ...

  2. 【PAT甲级】1099 Build A Binary Search Tree (30 分)

    一.题目分析 1. 翻译 recursively:递归地 property:性质 2. 关键点 1)二叉排序树的中序遍历结果是递增排序的,因此直接中序遍历题目给出的二叉树,将排好序的数据填入后,层序遍 ...

  3. 【PAT (Advanced Level) Practice】1099 Build A Binary Search Tree (30 分)

    深搜+广搜 #include <iostream> #include <stack> #include <queue> #include <vector> ...

  4. 1099 Build A Binary Search Tree (30 分)【难度: 一般 / 知识点: 建立二叉搜索树】

    https://pintia.cn/problem-sets/994805342720868352/problems/994805367987355648 首先: 要知道二叉搜索树的中序遍历是有序的 ...

  5. PAT甲级——1099 Build A Binary Search Tree (二叉搜索树)

    本文同步发布在CSDN:https://blog.csdn.net/weixin_44385565/article/details/90701125 1099 Build A Binary Searc ...

  6. 1099. Build A Binary Search Tree (30)

    1099. Build A Binary Search Tree (30) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN ...

  7. C++学习之路 | PTA(甲级)—— 1064 Complete Binary Search Tree (30分)(带注释)(精简)

    1064 Complete Binary Search Tree (30分) A Binary Search Tree (BST) is recursively defined as a binary ...

  8. PAT 甲 1099 Build A Binary Search Tree

    2022.1.28 练习 PAT甲 1099 Build A Binary Search Tree (原题链接) 题解如下: #include <bits/stdc++.h> using ...

  9. PAT甲级1064 Complete Binary Search Tree (30分):[C++题解]完全二叉搜索树BST

    文章目录 题目分析 题目链接 题目分析 思路: 第一步,构造含有n个结点的完全二叉树:第二步,将n个数值填入,使其满足二叉搜索树的性质. 对于第一步: 完全二叉树用一维数组可以存下,不过从根结点的下标 ...

最新文章

  1. Post和Get方法区别
  2. ASP.NET的票据工具类FormsAuthenticationTicket
  3. PHP 判断数据类型
  4. k8s php mysql_在k8s上部署第一个php应用
  5. [集训队作业2018] count(笛卡尔树,生成函数,卡特兰数)
  6. SQL查询多条只取其中最新的一条数据
  7. Win32汇编---控件的超类化感想
  8. MATLAB 撰写word
  9. 人工智能 AI技术学习路线图 初阶+中阶+高阶
  10. 【心电信号】基于matlab GUI自适应滤波+平滑滤波+小波滤波心电信号处理【含Matlab源码 1809期】
  11. bootdo增加验证码登陆
  12. html语言中的字体代码,html中让字体变红的代码
  13. shell工具finalShell
  14. 【win10专业版】win10系统下Office2013无法激活的解决方法
  15. 优先级倒挂(priority inversion)
  16. Proxy returns “HTTP/1.1 407 Proxy Authentication Required
  17. python glob函数_Python glob()函数
  18. oracle数据库常用sql整理
  19. 2018首届传神者大会:“语言+新技术”将推动语言产业生态化发展
  20. 基于js原生算法+cocos游戏引擎+uni框架Cloud托管网页:开发2048小游戏域名发布版本

热门文章

  1. 论文浅尝 | 弱监督关系抽取的深度残差学习方法
  2. redis笔记_源码_双端链表list
  3. Serializer字段和选项
  4. JavaScript基本概念(下)
  5. 【Tech】Mac上安装MAMP打开本地网页
  6. javascript入门视频第一天 小案例制作 零基础开始学习javascript
  7. Ubuntu10.04下安装Ns2的一系列错误及解决方案
  8. 使用ABAP delete duplicate时遇到的问题
  9. windows 堆栈溢出简易测试代码
  10. JVM学习笔记-04-java历史-沙箱安全机制