分析:

  考察BST + 完全二叉树的性质,注意:

    (1):先用排序排好,然后由于是完全二叉树,我们使用中序来建树。

    (2):建好之后,层次遍历可以采用队列。

 #include <iostream>
#include <stdio.h>
#include <algorithm>
#include <cstring>
#include <vector>
#include <queue>
#include <cmath> using namespace std; vector<int> vect;
vector<int> result; struct Node
{
int value;
Node *left;
Node *right;
}*root; Node *In(int level, int ll, int rr)
{
if (ll > rr) return NULL;
int up_level = pow(, (level - )) - ; //除最后一层外 总共有多少个
int Last_level_left = (rr - ll) + - up_level; //最后一层剩下的 int k = pow(, (level - ) - ); //最后一层是否布满了左分支 int left_left;
if (Last_level_left >= k) left_left = up_level;
else left_left = pow(, (level - )) - + Last_level_left; Node *p = new Node();
p->value = vect[left_left + ll]; p->left = In(level - , ll, ll + left_left - );
p->right = In(level - , ll + left_left + , rr); return p;
} void level_order(Node *root)
{
queue<Node *> q;
q.push(root); while (!q.empty())
{
Node *p = q.front();
q.pop(); result.push_back(p->value); if (p->left != NULL)
q.push(p->left);
if (p->right != NULL)
q.push(p->right);
} printf("%d", result[]);
for (int i = ; i < result.size(); i++)
printf(" %d", result[i]);
printf("\n");
} int main()
{
int n; while (cin >> n)
{
vect.clear();
result.clear();
for (int i = ; i < n; i++)
{
int k;
cin >> k;
vect.push_back(k);
}
sort(vect.begin(), vect.end()); int level = log2(n) + ; root = In(level, , n - );
level_order(root); }
return ;
}

1064. Complete Binary Search Tree (30)的更多相关文章

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

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

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

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

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

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

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

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

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

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

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

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

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

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

  9. PAT (Advanced Level) 1064. Complete Binary Search Tree (30)

    因为是要构造完全二叉树,所以树的形状已经确定了. 因此只要递归确定每个节点是多少即可. #include<cstdio> #include<cstring> #include& ...

随机推荐

  1. [家里蹲大学数学杂志]第235期$L^p$ 调和函数恒为零

    设 $u$ 是 $\bbR^n$ 上的调和函数, 且 $$\bex \sen{u}_{L^p}=\sex{\int_{\bbR^n}|u(y)|^p\rd y}^{1/p}<\infty. \e ...

  2. 源码阅读笔记 - 1 MSVC2015中的std::sort

    大约寒假开始的时候我就已经把std::sort的源码阅读完毕并理解其中的做法了,到了寒假结尾,姑且把它写出来 这是我的第一篇源码阅读笔记,以后会发更多的,包括算法和库实现,源码会按照我自己的代码风格格 ...

  3. java Util

    import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONObject; import com.qihangedu.tms.a ...

  4. Python基础篇【第2篇】: Python内置函数(一)

    Python内置函数 lambda lambda表达式相当于函数体为单个return语句的普通函数的匿名函数.请注意,lambda语法并没有使用return关键字.开发者可以在任何可以使用函数引用的位 ...

  5. windows Server 2008 IE增强的安全配置关闭方法

    解决方法 开始->管理工具->服务器管理器

  6. [VB.NET]调用系统的文件夹选择对话框

    以下示例代码展示如何调用系统的文件夹选择对话框: Private Function SelectFolder(ByVal Describe As String, Optional ByVal Show ...

  7. pow的小事不简单

    http://acm.hdu.edu.cn/showproblem.php?pid=5878 #include<stdio.h> #include<iostream> #inc ...

  8. linux下重启tomcat,日志查看

    版权声明:本文为楼主原创文章,未经楼主允许不得转载,如要转载请注明来源. 一:关闭tomcat 1.使用cd命令以及常用的tab补全命令进入到tomcat bin所在的目录(可以不用到此目录也行,楼主 ...

  9. 网站接入QQ登录的两种方法

    第一种使用系统自带按钮登录,但是这样会弹出新窗口,不喜欢 //调用QC.Login方法,指定btnId参数将按钮绑定在容器节点中 QC.Login({ //btnId:插入按钮的节点id,必选 btn ...

  10. MVC导出Excel,提供下载Excel

    类1: using System.Collections.Generic;using System.Data;using System.Web.Mvc;using System.IO;using Sy ...