由于是满二叉树,用数组既可以表示
父节点是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. 最短路径Dijkstra matlab

    Dijkstra: function [dist,pre, full_path]=MinRoad_Dijkstra(G,v0) n=0; if isfield(G,'w') && ~i ...

  2. linux命令1—安装optimizer

    ZendChina官方:下面介绍一下关于在linux环境下Zend Optimizer 3.3的安装方法.本篇文章是基于RHEL5架构的linux系统. (1)ZendOptimizer 3.3.3版 ...

  3. ArcGIS Earth1.9最新版安装和使用教程

    1.下载ArcGIS Earth 官网下载地址:https://www.esri.com/en-us/arcgis/products/arcgis-earth 在这个网页的最下面填上信息,就可以下载了 ...

  4. SDN 第二次作业

    问题 1.为什么需要SDN?SDN特点? 答:当今网络快速发展,用户的需求也就日益增加,但网络的创新速度却并没有增加,而是比较缓慢.传统网络中的网络设备是硬件.操作系统.网络应用紧耦合的,每个设备厂商 ...

  5. Python csv.md

    csv csv模块可以用于处理从电子表格和数据库导出的数据到带有字段和记录格式的文本文件,通常称为逗号分隔值(csv)格式,因为逗号通常用于分隔记录中的字段. Reading csv.reader(c ...

  6. 关于ARMv8指令的几个问题

    版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/qianlong4526888/article/details/27512629 NOTE:下面内容仅 ...

  7. FFT && NTT板子

    贴板子啦-- FFT板子:luogu P3803 [模板]多项式乘法(FFT) #include<cstdio> #include<iostream> #include< ...

  8. oracle查询父节点及其下所有子节点

    1.我们的组织机构就是这种树形菜单的格式. . 2.执行sql: select ( select organization_name from SYS_ORGANIZATION where organ ...

  9. css盒子模型(box-sizing)

    盒子模型 关于CSS重要的一个概念就是CSS盒子模型.它控制着页面这些元素的高度和宽度.盒子模型多少会让人产生一些困惑,尤其当涉及到高度和宽度计算的时候.真正盒子的宽度(在页面呈现出来的宽度)和高度, ...

  10. P1821 [USACO07FEB]银牛派对Silver Cow Party

    题目描述 One cow from each of N farms (1 ≤ N ≤ 1000) conveniently numbered 1..N is going to attend the b ...