由于是满二叉树,用数组既可以表示
父节点是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. MySQL基础之 如何删除主键

    我们在一个表中设置了主键之后,那么如何删除主键呢? 删除主键的语法是: ALTER TABLE TABLE_NAME DROP PRIMARY KEY; 在这里我们要考虑两种情况: 1.可以直接使用d ...

  2. [python]通过uiautomator实现返回当前程序包名

    # -*- coding: utf-8 -*- from uiautomator import device as d def getCurrentPackageName(): info = d.in ...

  3. BZOJ2306:[CTSC2011]幸福路径(倍增Floyd)

    Description 有向图 G有n个顶点 1,  2, …,  n,点i 的权值为 w(i).现在有一只蚂蚁,从给定的起点 v0出发,沿着图 G 的边爬行.开始时,它的体力为 1.每爬过一条边,它 ...

  4. oracle 查看删除重复数据

    1.查询重复数据select * from 表名 where 重复字段(一般为主键)in (select 重复字段 from 表名 group by 重复字段 having count(WF_OID) ...

  5. day3-课堂笔记

    函数有2种: 1种是有返回值的,不改变对象本身 1种是没有返回值的,改变对象本身   eval函数就是实现list.dict.tuple与str之间的转化(只能把类似格式的字符串进行相应转换)str函 ...

  6. Mysql 调优2个语句

    一.explain 语句 查看语句的执行计划 二.查看具体每一步耗时 .; .执行SQL .show profiles; 获取2执行SQL的query_id .show profile for que ...

  7. Linux中Shell

    Linux中Shell Shell是什么 ​ Shell是一个命令行解释器,为用户提供了一个向Linux内核发送请求以便运行程序的界面系统级程序,可以用Shell来启动.挂起.停止.编写一些程序. S ...

  8. 突然的明白--public static 类名 函数名()

    public static ImageUtilEngine getImageEngine() { return imageEngine; } 这个是什么啊........纠结了一个多星期的东西 忽然间 ...

  9. UML类图(Unified Modeling Language Class Diagrams)

    统一建模语言(UML) |  类图 什么是UML? UML是一种用于可视化描述系统,具有广泛用途的建模语言.作为一种标准化的图形语言,在软件工业中被用于软件系统部件的具体化,可视化,结构化描述以及撰写 ...

  10. DDD China Conference 2017

    DDD China Conference 2017 时间:2017.12.9地点:国际会议中心