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 (≤). 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

题意:

给出二叉搜索树的一个序列,如果这棵树还要求是完全二叉树的话,这棵树就是唯一的。现在要输出这棵树的层序遍历。

题解:

看了别人的代码,也不是很能理解。。。。

一棵排序二叉树的中序遍历就是这一组数的递增序列。

这边是完全二叉树,假设从0开始,那么节点i的左孩子的标号就是2*i+1,右孩子的标号就是2*(i+1)。

先将这组数按照递增来排序,然后用中序遍历复原这棵完全排序二叉树,最后直接输出。

完全二叉排序树按层序存放在从1开始的数组中,左右儿子节点的索引分别为(根节点索引假设为root)root*2和root*2+1。而二叉排序树的中序遍历是递增的。我们现在把输入序列排序,就可以得到中序遍历的序列了。于是我们开始遍历这棵左右儿子节点的索引分别为(根节点索引假设为root)root*2和root*2+1的空树,与以往一边遍历一边打印不同的是,我们是一边遍历,一边给这棵空树赋值
很不错。学习这种思想。

AC代码:

#include<iostream>
#include<set>
#include<algorithm>
#include<string>
#include<cstring>
using namespace std;
int n, a[], b[], k = ;
void inorder(int root)
{
//cout<<"root:"<<root<<endl;
if (root<n)
{
inorder( * root + );
//printf("%d ==== %d ==== %d\n",root,k,a[k]);
b[root] = a[k++];
inorder( * root + );
}
} int main()
{
cin >> n;
for (int i = ; i<n; i++)
cin >> a[i];
sort(a, a + n); //从小到大排序 因为完全二叉搜索树的中序遍历就是从小到大排序
inorder();
for (int i = ; i<n - ; i++)
cout << b[i] << " ";
cout << b[n-] << endl;
return ;
}


root:
root:
root:
root:
root:
==== ====
root:
==== ====
root:
root:
==== ====
root:
==== ====
root:
root:
root:
==== ====
root:
==== ====
root:
==== ====
root:
root:
root:
==== ====
root:
==== ====
root:
root:
==== ====
root:

PAT 甲级 1064 Complete Binary Search Tree (30 分)(不会做,重点复习,模拟中序遍历)的更多相关文章

  1. pat 甲级 1064. Complete Binary Search Tree (30)

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

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

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

  3. pat 甲级 1064 ( Complete Binary Search Tree ) (数据结构)

    1064 Complete Binary Search Tree (30 分) A Binary Search Tree (BST) is recursively defined as a binar ...

  4. PAT 甲级 1064 Complete Binary Search Tree

    https://pintia.cn/problem-sets/994805342720868352/problems/994805407749357568 A Binary Search Tree ( ...

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

  6. 1064 Complete Binary Search Tree (30分)(已知中序输出层序遍历)

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

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

    题意:输入一个正整数N(<=1000),接着输入N个非负整数(<=2000),输出完全二叉树的层次遍历. AAAAAccepted code: #define HAVE_STRUCT_TI ...

  8. PAT题库-1064. Complete Binary Search Tree (30)

    1064. Complete Binary Search Tree (30) 时间限制 100 ms 内存限制 32000 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHE ...

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

随机推荐

  1. 学习Spring-Data-Jpa(四)---Naming命名策略,源码跟踪

    1.首先在Entity实体中,命名方式有两种: 一种是显示命名,即通过@Table的name属性指定对应的数据库表名称,@Column的name属性指定实体字段对应数据库字段的名称. 另一种是隐式命名 ...

  2. MongoDB dataSize如何比storageSize更大?

      原文   https://stackoverflow.com/questions/34054780/how-can-mongodb-datasize-be-larger-than-storages ...

  3. COM Error---HRESULT

    一.COM Error 调用COM接口产生的错误.几乎所有的COM函数和接口方法都返回类型为HRESULT的值.HRESULT(用于结果句柄)是返回成功.警告和错误值的一种方法.HRESULTs实际上 ...

  4. AtCoder Grand Contest 013题解

    传送门 \(A\) 先把相同的缩一起,然后贪心就可以了 //quming #include<bits/stdc++.h> #define R register #define fp(i,a ...

  5. AtCoder Grand Contest 012题解

    传送门 \(A\) 肯定是后面每两个陪最前面一个最优 typedef long long ll; const int N=5e5+5; int a[N],n;ll res; int main(){ s ...

  6. Java中的读文件,文件的创建,写文件

    前言 大家好,我是 Vic,今天给大家带来Java中的读文件,文件的创建,写文件的概述,希望你们喜欢 示意图 读文件 public static void read(String path,Strin ...

  7. C语言实现多线程排序

    #include <stdio.h> #include <pthread.h> #include <stdlib.h> #include <string.h& ...

  8. 超炫酷的 Docker 终端 UI lazydocker

    A simple terminal UI for both docker and docker-compose, written in Go with the gocui library. https ...

  9. 获取句柄的类型以及对应的ID序号

    遍历所有进程下的所有句柄,以及对应句柄类型. 一丶简介 在有的时候.我们会需要对应句柄名字.以及句柄类型的名称. 以及它所对应的的ID. 因为每个系统不一样.所以每次都是不一样的. 有的时候我们就需要 ...

  10. redis应用场景,缓存的各种问题

    缓存 redis还有另外一个重要的应用领域——缓存 引用来自网友的图解释缓存在架构中的位置 默认情况下,我们的服务架构如下图,客户端请求service,然后service去读取mysql数据库 问题存 ...