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<bits/stdc++.h>
using namespace std;
struct node {node* lc;node* rc;int val;
}nd[200];
int a[200];
int cnt = 0;
queue<node>q;
void midsearch(node *point)
{if (point == nullptr)return;midsearch(point->lc);point->val = a[cnt++];midsearch(point->rc);
}
void bfs()
{q.push(*nd);int flag = 0;while (!q.empty()){if (flag)cout << ' ';flag = 1;node tem = q.front();cout << tem.val;q.pop();if (tem.lc != nullptr)q.push(*(tem.lc));if (tem.rc != nullptr)q.push(*(tem.rc));}
}
int main()
{int n;cin >> n;for (int i = 0; i < n; i++){int l, r;cin >> l >> r;if (l != -1)nd[i].lc = nd + l;if (r != -1)nd[i].rc = nd + r;}for (int i = 0; i < n; i++)cin >> a[i];sort(a, a + n);midsearch(nd);bfs();
}

PAT 1099 Build A Binary Search Tree相关推荐

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

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

  2. PAT 甲 1099 Build A Binary Search Tree

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

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

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

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

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

  5. 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 ...

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

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

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

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

  8. 1099 Build A Binary Search Tree

    1. 本题给出了树的样子,给出了用来填充的数列,并且告诉是一棵二叉查找树. 2. 先用静态存储的方式将树的框架建立起.然后对数列进行小到大排序,利用BST中序遍历是升序的性质,通过中序遍历将数值填充的 ...

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

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

最新文章

  1. 博世投资张翠波:自动驾驶需要突破芯片、传感器、深度学习算法等关键技术
  2. 编程问题之:x=xamp;(x-1)
  3. 翻译java_翻译示例代码
  4. gzencode php demo,8个必备的PHP功能实例代码
  5. mysql 全值匹配什么意思
  6. 十四、linux 静态/动态申请字符设备号
  7. 数据类型之Integer与int
  8. 小程序css之圆角边框
  9. linux的基本使用方法,Linux/Unix基本使用方法
  10. java------用File类列出一个目录下的所有文件夹和文件
  11. CCF201903-5 317号子任务(100分题解链接)
  12. linux清指令,Linux隐匿和清除的几条命令
  13. python编译器大全_Python编译器
  14. 广东电网公司大数据平台初步建成
  15. 英特尔12代酷睿处理器强势来袭
  16. html消除样式,清除css样式
  17. windows下如何下载g++以及gcc
  18. 学习笔记--STM32F205+LCD
  19. android平板改成电视盒子,自己动手把闲置的手机或者平板自制成电视的盒子
  20. QT 自定义类访问UI控件的几种方法

热门文章

  1. c语言成绩查询系统源代码,C语言版简单成绩查询系统源代码概要.doc
  2. MLlib spark 垃圾邮件分类
  3. 全球直播的罗胖跨年演讲背后技术支撑故事——罗辑思维首席架构师方圆访谈
  4. 【调剂】黑龙江科技大学2021年硕士研究生招生预调剂政策
  5. 部分银行核心系统的资料 【转】
  6. Apollo:Routing模块
  7. BackTrader:性能优化之多股策略速度优化
  8. 字节跳动 CEO 梁汝波发内部信:头条、西瓜、搜索等业务并入抖音
  9. 湖南大学第十六届程序设计竞赛(重现赛)补题 A
  10. 以我所能,为你而加 —— 新一代音视频融合通信平台全接触