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 ...
随机推荐
- 动态代理Dynamic Proxy
代理模式是常用的Java设计模式,他的特征是代理类与委托类有同样的接口,代理类主要负责为委托类 预处理消息,过滤消息,把消息转发给委托类,以及事后处理消息等. 代理类与委托类之间通常会存在关联关系,一 ...
- H2O 笔记之安装
参考资料: 了解H2O:http://h2o-release.s3.amazonaws.com/h2o/rel-turchin/9/docs-website/h2o-docs/index.html 安 ...
- day2_抓包-抓包工具Charles
1.Charles功能简单描述 1)定位问题,前端的.后端的问题 2)发出去的请求,请求头.请求体,返回的数据 3)拦截请求,修改请求 2.Charles抓包(Android手机) 1.要求手机得和你 ...
- LeetCode 617 Merge Two Binary Trees 解题报告
题目要求 Given two binary trees and imagine that when you put one of them to cover the other, some nodes ...
- 使用Git,如何忽略不需要上传的文件(配置文件)
步骤1:在目录下,选择GIt Bash Here 2.输入命令 : git update-index --assume-unchanged 文件名 3.再输入指令 git status 查看修改文件 ...
- GIt如何安装使用
一:公式git服务器地址:192.168.1.16 . 采用https协议,建议大家编辑本机hosts文件,将此地址映射到域名git.penseesoft.com,已防止出现的SSL证书警告. Hos ...
- mysql之show engine innodb status解读(转)
add by zhj: 我第一次知道这个命令是线上服务出了问题,然后同事用这个命令去查看死锁.但用这个命令看死锁有一定的局限性,它只能看到最后一次死锁, 而且只能看到死锁环中的两个事务所执行的最后一条 ...
- 高性能网络编程7--tcp连接的内存使用
滑动窗口的工作方式 窗口通知: 发送端维护发送窗口大小(不在包中传输),接收端在ACK中告知接收窗口大小: 发送窗口初始是发送缓冲区大小,接收窗口初始是接收缓冲区大小:缓冲区决定窗口的最大值: 发送窗 ...
- dwr的ScriptSession和HttpSession分析
1.关于ScriptSession ScriptSession不会与HttpSession同时创建 当我们访问一个页面的时候,如果是第一次访问,会创建一个新的HttpSession,之后再访问的时候, ...
- mysql实时增量备份
采用binlog日志的好处 掌控所有更改操作,必要时可用于恢复数据 数据库主从复制的必要条件 [root@localhost~]# vim /etc/my.cnf [mysqld] .. .. log ...