pat 甲级 1064. Complete Binary Search Tree (30)
1064. Complete 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.
A Complete Binary Tree (CBT) is a tree that is completely filled, with the possible exception of the bottom level, which is filled from left to right.
Now given a sequence of distinct non-negative integer keys, a unique BST can be constructed if it is required that the tree must also be a CBT. You are supposed to output the level order traversal sequence of this BST.
Input Specification:
Each input file contains one test case. For each case, the first line contains a positive integer N (<=1000). Then N distinct non-negative integer keys are given in the next line. All the numbers in a line are separated by a space and are no greater than 2000.
Output Specification:
For each test case, print in one line the level order traversal sequence of the corresponding complete binary search tree. All the numbers in a line must be separated by a space, and there must be no extra space at the end of the line.
Sample Input:
10
1 2 3 4 5 6 7 8 9 0
Sample Output:
6 3 8 1 5 7 9 0 2 4 先按照定义构建完全二叉树的框架,接下来就是把数字分别填到这颗二叉树的节点中,使得其满足二叉搜索树的定义。其实填入节点可以直接中序遍历这颗二叉树框架即可。
AC代码:
#define _CRT_SECURE_NO_DEPRECATE
#pragma warning(disable:4996)
#include<iostream>
#include<string>
#include<algorithm>
#include<map>
#include<cctype>
#include<cmath>
#include<cstring>
#include<vector>
#include<set>
#include<queue>
#include<limits.h>
using namespace std;
typedef long long ll;
#define N_MAX 1000+5
#define INF 0x3f3f3f3f
int n, a[N_MAX];
struct Node {
int key;
int l_child, r_child;
Node(int key=INF,int l_child=INF,int r_child=INF):key(key),l_child(l_child),r_child(r_child) {}
}node[N_MAX];
void init(int n) {//建立完全二叉树的框架
n--;
int cur = ;
int flag = ;
while (n--) {
if (!(flag & ))node[cur].l_child = * cur + ;
else {
node[cur].r_child = *cur+;
cur++;
}
flag++;
}
} int cnt = ;
void inorder(int n) {
if(node[n].l_child!=INF)inorder(node[n].l_child);
node[n].key = a[cnt++];
if(node[n].r_child!=INF)inorder(node[n].r_child);
}
vector<int>vec;
void bfs() {
queue<int>que;
que.push();
while (!que.empty()) {
int p = que.front(); que.pop();
vec.push_back(node[p].key);
if (node[p].l_child != INF)que.push(node[p].l_child);
if (node[p].r_child != INF)que.push(node[p].r_child);
}
}
int main() {
while (scanf("%d", &n) != EOF) {
for (int i = ; i < n; i++) {
scanf("%d", &a[i]);
}
sort(a,a+n);
init(n);
cnt = ;
inorder();
bfs();
for (int i = ; i < vec.size();i++) {
printf("%d%c",vec[i],i+==vec.size()?'\n':' ');
}
}
return ;
}
pat 甲级 1064. Complete Binary Search Tree (30)的更多相关文章
- PAT 甲级 1064 Complete Binary Search Tree (30 分)(不会做,重点复习,模拟中序遍历)
1064 Complete Binary Search Tree (30 分) A Binary Search Tree (BST) is recursively defined as a bin ...
- pat 甲级 1064 ( Complete Binary Search Tree ) (数据结构)
1064 Complete Binary Search Tree (30 分) A Binary Search Tree (BST) is recursively defined as a binar ...
- PAT 甲级 1064 Complete Binary Search Tree
https://pintia.cn/problem-sets/994805342720868352/problems/994805407749357568 A Binary Search Tree ( ...
- PAT Advanced 1064 Complete Binary Search Tree (30) [⼆叉查找树BST]
题目 A Binary Search Tree (BST) is recursively defined as a binary tree which has the following proper ...
- PAT甲级:1064 Complete Binary Search Tree (30分)
PAT甲级:1064 Complete Binary Search Tree (30分) 题干 A Binary Search Tree (BST) is recursively defined as ...
- PAT题库-1064. Complete Binary Search Tree (30)
1064. Complete Binary Search Tree (30) 时间限制 100 ms 内存限制 32000 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHE ...
- 1064. Complete Binary Search Tree (30)【二叉树】——PAT (Advanced Level) Practise
题目信息 1064. Complete Binary Search Tree (30) 时间限制100 ms 内存限制65536 kB 代码长度限制16000 B A Binary Search Tr ...
- PAT甲级——A1064 Complete Binary Search Tree
A Binary Search Tree (BST) is recursively defined as a binary tree which has the following propertie ...
- 1064 Complete Binary Search Tree (30分)(已知中序输出层序遍历)
A Binary Search Tree (BST) is recursively defined as a binary tree which has the following propertie ...
随机推荐
- Java基础面试题:String 和StringBuffer的区别
package com.swift; import java.util.Date; public class Getclass_Test extends Date { public static vo ...
- jsp 生成验证码代码
调用方法:在jsp页面用图像标签便可以直接调用如下是标签代码<img border=0 src="image.jsp">,只需要把该代码发在验证码要显示的区域就可以了) ...
- Linux系统故障分析与排查--日志分析
处理Linux系统出现的各种故障时,故障的症状是最先发现的,而导致这以故障的原因才是最终排除故障的关键.熟悉Linux系统的日志管理,了解常见故障的分析与解决办法,将有助于管理员快速定位故障点,“对症 ...
- DevOps - 日志分析 -ELK
wget --no-check-certificate --no-cookies --header "Cookie: oraclelicense=accept-securebackup-co ...
- 设置虚拟机里的Centos7的IP
输入ip查询命名 ip addr 也可以输入 ifconfig查看ip,但此命令会出现3个条目,centos的ip地址是ens33条目中的inet值. 发现 ens33 没有 inet 这个属性,那 ...
- MYSQL 自定义排序
在mysql order by排序中,大多数情况下仅使用默认排序规则就够了:字符串按字典顺序,数字按大小等等.可有时候,某个字段是有自身业务含义的,比如 type(1,2,3)可能表示早/中/晚,如果 ...
- 转发一个关于下载qq无损音乐的博客
import requests import json headers = { 'Host': 'c.y.qq.com', 'Referer': 'http://c.y.qq.com/', 'User ...
- 必须使用member initialization list来初始化的情况
// member initialization #include <iostream> using namespace std; class Circle { double radius ...
- Keywords Search HDU - 2222 ( ac自动机)模版题
Keywords Search Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others ...
- DiyCode开源项目 BaseActivity 分析
1.首先将这个项目的BaseActivity源码拷贝过来. /* * Copyright 2017 GcsSloop * * Licensed under the Apache License, Ve ...