PAT 64.Complete Binary Search Tree
题目链接: http://pat.zju.edu.cn/contests/pat-a-practise/1064
思路分析:
1)先对数组排好序。
2)采用中序遍历的方式,将排好序的元素逐个插入在完全二叉树中。
3)利用完全二叉树采用数组存储的方式,对于结点序号为index的结点,其左孩子结点2*i,右孩子结点为2*i+1,(结点编号从1开始)遍历。
#include <stdio.h>
#include <stdlib.h>
#include <algorithm>
using namespace std; int t[],a[];
int m=; void inOrder(int index){
if((index*)<=n) inOrder(*index);
t[index]=a[m++];
if((index*+)<=n) inOrder(*index+);
} int main(){
int n;
scanf("%d",&n); for(int i=;i<=n;i++){
scanf("%d",&a[i]);
} sort(a+,a+n+);
inOrder(); for(int i=;i<=n;i++){
printf("%d",t[i]);
if(i!=n) printf(" ");
} system("pause");
return ;
}
PAT 64.Complete Binary Search Tree的更多相关文章
- PAT 1064 Complete Binary Search Tree[二叉树][难]
1064 Complete Binary Search Tree (30)(30 分) A Binary Search Tree (BST) is recursively defined as a b ...
- PAT 1064 Complete Binary Search Tree
#include <iostream> #include <cstdio> #include <cstdlib> #include <vector> # ...
- PAT_A1064#Complete Binary Search Tree
Source: PAT A1064 Complete Binary Search Tree (30 分) Description: A Binary Search Tree (BST) is recu ...
- 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 ...
- PAT 甲级 1064 Complete Binary Search Tree
https://pintia.cn/problem-sets/994805342720868352/problems/994805407749357568 A Binary Search Tree ( ...
- pat 甲级 1064. Complete Binary Search Tree (30)
1064. Complete Binary Search Tree (30) 时间限制 100 ms 内存限制 65536 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 甲级 1064 Complete Binary Search Tree (30 分)(不会做,重点复习,模拟中序遍历)
1064 Complete Binary Search Tree (30 分) A Binary Search Tree (BST) is recursively defined as a bin ...
随机推荐
- jQuery Mobile 网格
在列容器中,根据不同的列数,子元素可设置类 ui-block-a|b|c|d|e.这些列将依次并排浮动. 网格中的列是等宽的(总宽是 100%),无边框.背景.外边距或内边距. 例: 对于 ui-gr ...
- Lintcode--005(最长公共子序列)
Given two strings, find the longest common subsequence (LCS). 最长公共子序列 Your code should return th ...
- Express4+Mongodb极简入门实例
一.准备工作: 1.启动mongodb:bin目录下运行 2.在test数据库里插入一条数据: 二.正式开始: 1.通过应用生成器工具 express 快速创建一个应用的骨架,参考Express中文网 ...
- 配置Android SDK 开发环境(转)
1. 下载Eclipse 在前面我们配置好了JDK环境后,就可以开始配置Android的集成开发环境了,官方Google推荐的集成开发环境为Eclipse,所以我们就以Eclipse作为集成开发环境. ...
- ubuntu apache svn 参考
Ubuntu下Subversion服务器的安装配置 本文涉及的范围 要通过 HTTP 协议访问 Subversion 文件仓库,需要安装并配置好 Web 服务器.Apache2 被证实可以很好 ...
- cf B. Berland Bingo
http://codeforces.com/contest/370/problem/B 题意:给你n个卡片,卡片上有m个不同的数字,这个游戏是随即的从袋子里面抽球,球上有数字1-100:如果第ith玩 ...
- LeetCode_Unique Paths II
Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How m ...
- 解决比较Oracle中CLOB字段问题
解决比较Oracle中CLOB字段问题 Oracle中CLOB和BLOB字段虽说在开发中满足了存放超大内容的要求,但是在一些简单使用中确频频带来麻烦.CLOB中存放的是指针,并不能直接取到实际值. ...
- HDU4821---字符串hash,map判重
这是2013年长春区域赛的铜牌题...然而第一次做的时候一直觉得会超时的..最后才知道并没有想象中的那么恐怖: 这题有两个注意的地方: (1)h[i] = h[i-1] * seed + s[i] - ...
- Java 内存区域和GC机制-java概念理解
推荐几篇关于java内存介绍的文章 Java 内存区域和GC机制 http://www.cnblogs.com/hnrainll/archive/2013/11/06/3410042.html ...