PAT 1099 Build A Binary Search Tree
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相关推荐
- PAT甲级1099 Build A Binary Search Tree (30分):[C++题解]建立二叉搜索树、dfs和bfs
文章目录 题目分析 题目链接 题目分析 题意重述:给定一棵二叉树的结构,和待填的数值,请将数据填到二叉树中的结点中,使之满足二叉搜索树的性质. 然后按照层序遍历输出数值. 分析: 本题分两步. 第一步 ...
- PAT 甲 1099 Build A Binary Search Tree
2022.1.28 练习 PAT甲 1099 Build A Binary Search Tree (原题链接) 题解如下: #include <bits/stdc++.h> using ...
- PAT甲级——1099 Build A Binary Search Tree (二叉搜索树)
本文同步发布在CSDN:https://blog.csdn.net/weixin_44385565/article/details/90701125 1099 Build A Binary Searc ...
- 1099. Build A Binary Search Tree (30)
1099. Build A Binary Search Tree (30) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN ...
- 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 ...
- 【PAT (Advanced Level) Practice】1099 Build A Binary Search Tree (30 分)
深搜+广搜 #include <iostream> #include <stack> #include <queue> #include <vector> ...
- 【PAT甲级】1099 Build A Binary Search Tree (30 分)
一.题目分析 1. 翻译 recursively:递归地 property:性质 2. 关键点 1)二叉排序树的中序遍历结果是递增排序的,因此直接中序遍历题目给出的二叉树,将排好序的数据填入后,层序遍 ...
- 1099 Build A Binary Search Tree
1. 本题给出了树的样子,给出了用来填充的数列,并且告诉是一棵二叉查找树. 2. 先用静态存储的方式将树的框架建立起.然后对数列进行小到大排序,利用BST中序遍历是升序的性质,通过中序遍历将数值填充的 ...
- 1099 Build A Binary Search Tree (30 分)【难度: 一般 / 知识点: 建立二叉搜索树】
https://pintia.cn/problem-sets/994805342720868352/problems/994805367987355648 首先: 要知道二叉搜索树的中序遍历是有序的 ...
最新文章
- 博世投资张翠波:自动驾驶需要突破芯片、传感器、深度学习算法等关键技术
- 编程问题之:x=xamp;(x-1)
- 翻译java_翻译示例代码
- gzencode php demo,8个必备的PHP功能实例代码
- mysql 全值匹配什么意思
- 十四、linux 静态/动态申请字符设备号
- 数据类型之Integer与int
- 小程序css之圆角边框
- linux的基本使用方法,Linux/Unix基本使用方法
- java------用File类列出一个目录下的所有文件夹和文件
- CCF201903-5 317号子任务(100分题解链接)
- linux清指令,Linux隐匿和清除的几条命令
- python编译器大全_Python编译器
- 广东电网公司大数据平台初步建成
- 英特尔12代酷睿处理器强势来袭
- html消除样式,清除css样式
- windows下如何下载g++以及gcc
- 学习笔记--STM32F205+LCD
- android平板改成电视盒子,自己动手把闲置的手机或者平板自制成电视的盒子
- QT 自定义类访问UI控件的几种方法