https://pintia.cn/problem-sets/994805342720868352/problems/994805367987355648

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 (≤) which is the total number of nodes in the tree. The next Nlines 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 − 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 <bits/stdc++.h>
using namespace std; int N; struct Node{
int data;
int l, r;
}node[110]; vector<int> v;
int ans = 0; void inorder(int root) {
if(root == -1) return ; inorder(node[root].l);
node[root].data = v[ans ++];
inorder(node[root].r);
} void levelorder(int root) {
queue<int> q;
if(root != -1) q.push(root);
bool isfirst = true; while(!q.empty()) {
int t = q.front();
q.pop(); if(isfirst) {
printf("%d", node[t].data);
isfirst = false;
}
else printf(" %d", node[t].data); if(node[t].l != -1) q.push(node[t].l);
if(node[t].r != -1) q.push(node[t].r);
}
} int main() {
scanf("%d", &N);
for(int i =0; i < N; i ++)
scanf("%d%d", &node[i].l, &node[i].r);
v.resize(N);
for(int i = 0; i < N; i ++)
scanf("%d", &v[i]);
sort(v.begin(), v.end());
inorder(0);
levelorder(0);
return 0;
}

  这这这 应该是学会建树了叭 unbelieveable 有一点点鸡冻 

PAT 甲级 1099 Build A Binary Search Tree的更多相关文章

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

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

  2. pat 甲级 1099. Build A Binary Search Tree (30)

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

  3. PAT甲级——A1099 Build A Binary Search Tree

    A Binary Search Tree (BST) is recursively defined as a binary tree which has the following propertie ...

  4. PAT Advanced 1099 Build A Binary Search Tree (30) [⼆叉查找树BST]

    题目 A Binary Search Tree (BST) is recursively defined as a binary tree which has the following proper ...

  5. PAT 1099 Build A Binary Search Tree[BST性质]

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

  6. 1099 Build A Binary Search Tree

    1099 Build A Binary Search Tree (30)(30 分) A Binary Search Tree (BST) is recursively defined as a bi ...

  7. PAT甲级:1064 Complete Binary Search Tree (30分)

    PAT甲级:1064 Complete Binary Search Tree (30分) 题干 A Binary Search Tree (BST) is recursively defined as ...

  8. PAT (Advanced Level) Practise - 1099. Build A Binary Search Tree (30)

    http://www.patest.cn/contests/pat-a-practise/1099 A Binary Search Tree (BST) is recursively defined ...

  9. PAT 1099. Build A Binary Search Tree (树的中序,层序遍历)

    A Binary Search Tree (BST) is recursively defined as a binary tree which has the following propertie ...

随机推荐

  1. 简单记录一下ruby 循环

    今天整理一下ruby中的循环用法: 备注:“do~end”部分也可以写做{~} 1.break:直接跳出整个循环 i= 0 ["perl","python",& ...

  2. go语言Mutex与RWMutex用法

    Mutex(互斥锁) Lock()加锁,Unlock()解锁 适用于读写不确定,并且只有一个读或者写的场景 例: package main import ( "sync" &quo ...

  3. js scroll nav

    http://jsfiddle.net/cse_tushar/Dxtyu/141/http://ironsummitmedia.github.io/startbootstrap-scrolling-n ...

  4. IDEA常见错误解决

    tomcat控制台乱码 在tomcat的edit configurations里加入参数:-Dfile.encoding=UTF-8   导入的项目在重写时报 @Override is not all ...

  5. 20155213 2016-2017-2《Java程序设计》第四周学习总结

    20155213 2016-2017-2<Java程序设计>第四周学习总结 教材学习内容总结 继承与多态 继承 继承避免多个类间重复定义共同行为,使用关键字extends.继承表明了子类与 ...

  6. CF 833 B. The Bakery

    B. The Bakery http://codeforces.com/contest/833/problem/B 题意: 将一个长度为n的序列分成k份,每份的cost为不同的数的个数,求最大cost ...

  7. XAF-属性编辑器中的EditMask,DisplayFormat格式化字符串该如何设置

    XAF项目中有个DisplayFormat和EditMask设置,其中: 任何地方看到的DisplayFormat都是用于显示时,即非修改状态的编辑器,显示值的格式. EditMask是指编辑时的格式 ...

  8. android 图片二维码识别和保存(一)

    最新业务开发二维码识别的功能,这个功能,在很多应用上都有,比如微信长按图片识别二维码,如果图片中存在可以识别的二维码时,可以增加一个选项 识别二维码.那么如何去实现这个功能呢.这里其实也非常简单,首先 ...

  9. discuz修改附件出售用其他积分,与帖子不一样

    现实中我遇到了这种情况,一个资源可以用两种积分购买,于是我决定用售卖贴和出售附件的方式,附件内容与贴内隐藏内容是一样的,但目前discuz的出售主题和附件使用的是同一种积分,有了此修改 1.首先是显示 ...

  10. 003 -- Dubbo简单介绍

    1:Dubbo的基本概念 dubbo是阿里巴巴SOA服务治理 方案的核心框架,每天为20000+个服务次的数据量访问支持.dubbo是一个分布式的服务框架,致力于提供高性能和透明化的RPC远程服务调用 ...