04-树6 Complete Binary Search Tree(30 分)
title: 04-树6 Complete Binary Search Tree(30 分)
date: 2017-11-12 14:20:46
tags:
- 完全二叉树
- 二叉搜索树
categories: 数据结构
题目链接

题目大意
给出n个节点,构造一棵完全二叉搜索树。
最后按层次遍历输出这棵树。
分析
搜索二叉树的中序遍历是按照由小到大的顺序遍历的。
而完全二叉树,知道树的结点后就可以唯一确定树的结构,因此知道树的结点数后,中序遍历并将结点由小到大给树赋值,就可以构建出树。
AC代码
#include <bits/stdc++.h>
using namespace std;
int b[1023], a[1023];
int j=0, N;
void inOrder(int x){
if(x<=N){
//遍历左子树
inOrder(x + x);
//更新根节点
b[x] = a[j++];
//遍历右子树
inOrder(x+x+1);
}
}
int main(int argc, char const *argv[])
{
int i;
cin >> N;
for(i = 0; i<N; i++)
cin >> a[i];
sort(a, a + N);
inOrder(1);
cout << b[1];
for(i = 2; i<=N; i++)
cout << ' ' << b[i];
return 0;
}
04-树6 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 (30分)
PAT甲级:1064 Complete Binary Search Tree (30分) 题干 A Binary Search Tree (BST) is recursively defined as ...
- PTA 04-树6 Complete Binary Search Tree (30分)
题目地址 https://pta.patest.cn/pta/test/16/exam/4/question/669 5-7 Complete Binary Search Tree (30分) A ...
- 1064 Complete Binary Search Tree (30分)(已知中序输出层序遍历)
A Binary Search Tree (BST) is recursively defined as a binary tree which has the following propertie ...
- 04-树6 Complete Binary Search Tree (30 分)
A Binary Search Tree (BST) is recursively defined as a binary tree which has the following propertie ...
- 【PAT甲级】1064 Complete Binary Search Tree (30 分)
题意:输入一个正整数N(<=1000),接着输入N个非负整数(<=2000),输出完全二叉树的层次遍历. AAAAAccepted code: #define HAVE_STRUCT_TI ...
- PAT题库-1064. Complete Binary Search Tree (30)
1064. Complete Binary Search Tree (30) 时间限制 100 ms 内存限制 32000 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHE ...
- pat04-树8. Complete Binary Search Tree (30)
04-树8. Complete Binary Search Tree (30) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHE ...
- pat1064. Complete Binary Search Tree (30)
1064. Complete Binary Search Tree (30) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHE ...
- pat 甲级 1064. Complete Binary Search Tree (30)
1064. Complete Binary Search Tree (30) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHE ...
随机推荐
- day5_函数_判断小数
def check_float(s): ''' #这个函数的作用就是判断传入的字符串是否是合法的消失 :param s: 传入一个字符串 :return: True/False ''' s = str ...
- python学习目录(转载)
python基础篇 python 基础知识 python 初始python python 字符编码 python 类型及变量 python 字符串详解 python 列表详解 ...
- php 的Boolean类型
1. bool值不用区分大小写 $flag = Ture; $flag = TRUE $flag = true; 2. 其他类型在运算中转换为bool值 var_dump((bool) '0'); / ...
- Web前端性能优化策略
前端性能优化需要从前端的资源类型分析,以减少请求资源和请求时间为目的.目前的类型包括图片.javascript.css.动态数据等,不同的资源对于运算.带宽等的依赖也不同,因此优化的方式也不同.参照以 ...
- 【雅思】【写作】【大作文】Discuss both views and give your own opinion
•Discuss both views and give your own opinion • • •Agree or disagree •Discuss both views •Report ...
- MongoDB update修改器: 针对Fields的$修改器 $inc $set $unset
MongoDB update修改器: $inc $set $unset $push $pull $pop 针对Fields的$修改器 $set: { $set: { key: value } } $s ...
- 技巧:低版本VS打开高版本VS创建的工程
错误一:当用低版本VS打开高版本VS创建的工程时,会出现: 方案:将该工程的解决方案文件的后缀由xxx.sln改成了xxx.txt然后,查看其内容如下: Microsoft Visual Studio ...
- sublime使用手册
1.怎么批量选中开头和结尾?将光标定位到区域的开头,ctrl+alt+下键(一直按下键). 2.怎么打开和关闭tab的自动补全?preferences->settings->User{ & ...
- spring + mybatis配置及网络异常设置
Spring引入mybatis <beans xmlns="http://www.springframework.org/schema/beans" xmlns:contex ...
- 【LeetCode每天一题】Reverse Integer(反转数字)
Given a 32-bit signed integer, reverse digits of an integer. Example 1: ...