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 分)的更多相关文章

  1. PAT 甲级 1064 Complete Binary Search Tree (30 分)(不会做,重点复习,模拟中序遍历)

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

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

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

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

  5. 04-树6 Complete Binary Search Tree (30 分)

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

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

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

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

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

  8. pat04-树8. Complete Binary Search Tree (30)

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

  9. pat1064. Complete Binary Search Tree (30)

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

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

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

随机推荐

  1. tomcat闪退

    tomcat/bin/setclasspath.bat 用记事本打开 在setclasspath.bat的文件里添加两行代码,告诉tomcat,jdk和jre的位置(添加位置在: rem Make s ...

  2. zabbix zatree centos7安装zabbix-agent

    https://github.com/Emersonxuelinux/zatree-3.0-/tree/master/zabbix-3.0.x /bin/sh /config/ds.sh /tmp/z ...

  3. RHEL6.2的安装文档

    1 Installing RHEL 6.2 1.1 开始安装 选择“Install or upgrade an existing system”: 1.2 光盘检测 选择“Skip”跳过安装介质的检查 ...

  4. js一次控制 多个style样式

    ]; m.style.cssText='color:green;background:cyan;' 控制多个过渡效果 m.style.transition='opacity 1s ease-in,ba ...

  5. java JDBC (六) org.apache.commons.dbutils 增删改

    dbutils是apache封装了JDBC的工具类,比mysql-connector更方便些 下载地址:http://commons.apache.org/proper/commons-dbutils ...

  6. JavaScript学习(七)

  7. 第一节:Git下载和安装

    1.下载并安装github客户端:https://desktop.github.com/ 2.进入自己的主页,新建一个项目:

  8. 筛选BETWEEN '2018-1-1 00:00:00' AND '2018-5-18 00:00:00'每日`status`='1'的记录总条数

    最近做了一个小任务,要求是:使用MySQL #筛选BETWEEN '2018-1-1 00:00:00' AND '2018-5-18 00:00:00'每日`status`='1'的记录总条数 SE ...

  9. if __name__==__main__:的应用

    1. if __name__==__main__:代码的作用 python的文件有两种使用的方法:(1)直接作为脚本执行:(2)import到其他的python脚本中被调用(模块重用)执行: 因此if ...

  10. Caffe上用SSD训练和测试自己的数据

        学习caffe第一天,用SSD上上手. 我的根目录$caffe_root为/home/gpu/ljy/caffe    一.运行SSD示例代码    1.到https://github.com ...