Author: CHEN, Yue

Organization: 浙江大学

Time Limit: 200 ms

Memory Limit: 64 MB

Code Size Limit: 16 KB

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

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

Code

#include <stdio.h>
#include <stdlib.h>
#include <vector>
#include <queue>
#include <algorithm>
using namespace std;
const int maxn = 1010;
int n, data[maxn], left, right, index = 0;
vector<int> vnode[maxn], result;
typedef struct node{int data, right, left;
}node;
node nodes[maxn];
void inorder(int root){if (nodes[root].left != -1)inorder(nodes[root].left);nodes[root].data = data[index++];if (nodes[root].right != -1)inorder(nodes[root].right);
}
void levelorder(int root){queue<int> qi;qi.push(root);while (!qi.empty()){int now = qi.front();qi.pop();result.push_back(nodes[now].data);if (nodes[now].left != -1)    qi.push(nodes[now].left);if (nodes[now].right != -1)   qi.push(nodes[now].right);}
}
int main(){scanf("%d", &n);for (int i = 0; i < n; i++){scanf("%d %d", &left, &right);nodes[i].left = left;nodes[i].right = right;}for (int i = 0; i < n; i++)scanf("%d", &data[i]);sort(data, data + n);inorder(0);levelorder(0);for (int i = 0; i < n; i++){if (i)  printf(" ");printf("%d", result[i]);}printf("\n");return 0;
}

Analysis

-已知一颗二叉排序树的树型,并且给出n个key值。要求将这n个key值放入给出的树型中,且满足二叉排序树的要求。最后输出其层序遍历。

-使用中序遍历的思想,按照中序遍历的顺序向对应位置,按按序添入key值(先将key值按升序排列)。

【PAT】A1099. Build A Binary Search Tree (30)相关推荐

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

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

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

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

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

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

  4. PAT 甲 1099 Build A Binary Search Tree

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

  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(甲级)—— 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 ...

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

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

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

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

最新文章

  1. Yann LeCun推荐!自监督学习、全景FPN...内容平台的四大技术指南
  2. python自动测试g_使用Python进行自动化测试
  3. 《基于张量网络的机器学习入门》学习笔记2
  4. java web开发学习手册_Java 人必备学习手册开发下载!
  5. 6-1 线性表元素的区间删除 (10 分)
  6. Android系统搜索对话框(浮动搜索框)的使用
  7. 2018上IEC计算机高级语言(C)作业 第0次作业
  8. 3.5英寸万转硬盘的末路(万转“偏瘫”记连载二)
  9. poj Ancient Cipher 古代密码
  10. java jvm 1.6_JVM1.6 GC详解
  11. 连接linux服务器工具
  12. LSTM神经网络在证券市场分析上的应用
  13. HC-05与JDY-09蓝牙模块对比与使用
  14. USACO 4.2 The Perfect Stall 完美的牛栏(最大匹配)
  15. 盘点分布式文件存储系统
  16. 怒怼外媒,为中国正名,这个《流浪地球》捧红的犹太小哥太励志了
  17. 卓聚社区,新发现的全能社区
  18. sprintf() 用法
  19. 《Microduino实战》——2.3 Microduino STM32核心系列
  20. 京东云服务器使用教程

热门文章

  1. 【网络】https单向认证和双向认证
  2. 过去式与过去分词的用法与区别
  3. java鬼混笔记:springboot 5、springboot的Scheduled定时器:fixedDelay和fixedRate区别
  4. Android gridview keep item selected
  5. 阶乘因式分解(一)/java
  6. 高仿《ONE一个》安卓APP
  7. python高分书籍推荐_如果只推荐一本 Python 书,我要 Pick 它!
  8. Gym - 100519 B Bring Your Own Bombs 离散化+二分+思维
  9. 海量数据搜索算法优化-存储/查询/排序算法
  10. pixhawk连接到nvidia xavier