由于是满二叉树,用数组既可以表示
父节点是i,则左孩子是2*i,右孩子是2*i+1
另外根据二分搜索树的性质,中序遍历恰好是从小到大排序
因此先中序遍历填充节点对应的值,然后再层次遍历输出即可。

又是一道遍历的水题。。。

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <string.h>
#include <queue>
using namespace std;
const int maxn=;
int a[maxn];
int node[maxn];
int cnt=;
void build(int root,int n){
if(cnt>n)
return;
if(root>n)
return;
build(root<<,n);
node[root]=a[++cnt];
//printf("id:%d val:%d\n",root,node[root]);
build((root<<)|,n);
}
void BFS(int root,int n){
queue<int> q;
q.push(root);
int v;
bool first=true;
while(!q.empty()){
v=q.front();
q.pop();
if(first){
first=false;
printf("%d",node[v]);
}
else
printf(" %d",node[v]);
if(v*<=n)
q.push(v*);
if(v*+<=n)
q.push(v*+);
} }
int main()
{
int n;
scanf("%d",&n);
for(int i=;i<=n;i++)
scanf("%d",&a[i]);
sort(a+,a+n+);
build(,n);
BFS(,n); return ;
}

PAT甲题题解-1064. Complete Binary Search Tree (30)-中序和层次遍历,水的更多相关文章

  1. PAT甲题题解-1127. ZigZagging on a Tree (30)-中序、后序建树

    根据中序遍历和前序遍历确定一棵二叉树,然后按“层次遍历”序列输出.输出规则:除根节点外,接下来每层的节点输出顺序是:先从左到右,再从右到左,交替输出 #include <iostream> ...

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

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

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

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

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

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

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

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

  9. PAT甲题题解-1110. Complete Binary Tree (25)-(判断是否为完全二叉树)

    题意:判断一个节点为n的二叉树是否为完全二叉树.Yes输出完全二叉树的最后一个节点,No输出根节点. 建树,然后分别将该树与节点树为n的二叉树相比较,统计对应的节点个数,如果为n,则为完全二叉树,否则 ...

随机推荐

  1. 乘风破浪:LeetCode真题_031_Next Permutation

    乘风破浪:LeetCode真题_031_Next Permutation 一.前言 这是一道经典的题目,我们实在想不出最好的方法,只能按照已有的方法来解决,同时我们也应该思考一下为什么要这样做?是怎么 ...

  2. 修改TEMPDB所在的路径

    USE master go ALTER DATABASE tempdb MODIFY FILE (NAME = tempdev, FILENAME = 'Path\tempdb.mdf') go AL ...

  3. 在Linux服务器上运行Jupyter notebook server教程

    在Linux服务器上运行Jupyter notebook server教程 很多deep learning教程都推荐在jupyter notebook运行python代码,方便及时交互.但只在本地运行 ...

  4. animate is not a function(zepto 使用报错)[转]

    animate is not a function(zepto 使用报错) 1.为什么使用zepto写animate报错? 因为zepto默认构建包含: Core, Ajax, Event, Form ...

  5. MyBatis实战之动态SQL

    如果使用JDBC或者其他框架,很多时候你得根据需要去拼接SQL,这是一个麻烦的事情,而MyBatis提供对SQL语句动态的组装能力,而且它只有几个基本的元素,非常简单明了,大量的判断都可以在MyBat ...

  6. undefined symbol: ap_log_rerror;apache2.4与weblogic点so文件

    没法子啊:只能用 httpd-2.2.26 ============================== https://www.google.com.hk/#newwindow=1&q=un ...

  7. Python3.2-re模块之常用正则记录

    python的re模块是个很好的模块,这里简单记录下自己编写的几个有用的正则: 1:邮箱匹配: gReMailbox = re.compile(r'([\w\.\-+]+@[\w\-]+(?:\.[\ ...

  8. 多模匹配算法之Aho-Corasick

    除剔除那些含有敏感词的文本,由于有大量的敏感词,所以通过简单的正则表达式和字符串查找的方式效率太低,每次都有遍历一次字符串.而AC算法的核心思想就是避免不必要的回溯使搜索一直沿着向前的方向,最大可能的 ...

  9. array_multisort函数,以及多维数组下排序的应用,并与usort函数对比

    以前比较少用这个函数,大部分自己接触的业务里,处理稍微大一些的数组的时候几乎都是从db里取出来的,在db里就order by了. 最近倒是用了次,这个函数用来排序很强大,有点类似于sql中的order ...

  10. linux下通过软连接实现访问项目路径外面的资源

            在javaweb项目开发中,图片上传是个比较常见的场景.一般都是在项目路径下建个文件夹,然后上传到该文件夹下:这样这个图片就可以和静态资源一样被直接访问.这样的好处就是访问这图片特别方 ...