1064 Complete Binary Search Tree 甲级 xp_xht123

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

  • The left subtree of a node contains only nodes with keys less than the node's key.
  • The right subtree of a node contains only nodes with keys greater than or equal to the node's key.
  • Both the left and right subtrees must also be binary search trees.

A Complete Binary Tree (CBT) is a tree that is completely filled, with the possible exception of the bottom level, which is filled from left to right.

Now given a sequence of distinct non-negative integer keys, a unique BST can be constructed if it is required that the tree must also be a CBT. You are supposed to output the level order traversal sequence of this BST.

Input Specification:

Each input file contains one test case. For each case, the first line contains a positive integer N (≤1000). Then N distinct non-negative integer keys are given in the next line. All the numbers in a line are separated by a space and are no greater than 2000.

Output Specification:

For each test case, print in one line the level order traversal sequence of the corresponding complete binary search tree. All the numbers in a line must be separated by a space, and there must be no extra space at the end of the line.

Sample Input:

10
1 2 3 4 5 6 7 8 9 0

Sample Output:

6 3 8 1 5 7 9 0 2 4

解题思路:若父节点是x,那么左孩子是 2 * x,右孩子是 2 * x + 1

二叉树搜索树的中序遍历就是给定数组的递增顺序

所以,先排序再模拟中序遍历将逐个结果填进去即可的得到层序遍历结果

以下是完整代码:

//二叉树搜索树的中序遍历就是给定数组的递增顺序
/*
若父节点是x
那么左孩子是 2 * x
右孩子是 2 * x + 1
所以dfs从1开始
*/
#include<iostream>
#include<algorithm>

using namespace std;
const int N = 1010;
int n;
int inorder[N];
int res[N];
int k = 0;//中序遍历中的节点下标

void dfs(int u)
{
    //判断左子树是否越界
    if(u * 2 <= n) dfs(2 * u);
    res[u] = inorder[k ++];
    //判断右子树是否越界
    if(u * 2 + 1 <= n) dfs(2 * u + 1);
}

int main()
{
    cin>>n;
    for(int i = 0;i < n;i++) cin>>inorder[i];
    
    sort(inorder , inorder + n);
    
    dfs(1);
    
    for(int i = 1;i <= n;i++)
    {
        if(i != 1) cout<<" ";
        cout<<res[i];
    }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
源码简介: 1.网站信息设置:网站名称,网站地址,站长邮箱,备案ICP,站长QQ,版权信息。 2.广告管理:首页和分类页ads01-ads10 10个广告位置,可自由修改;首页推荐栏文字广告;竞价广告。 3.新站登陆打开与关闭(网站维护时可关闭)、新站登陆验证码识别 4.网站点入和点出统计 5.后台目录名字建议修改,以确保安全! 6.常用软件管理:内置16个软件链接,可以修改软件名字、地址、logo、排序等! 名站导航管理:内置53个名站链接,可以修改名站名字、地址、注释、颜色、排序等! 实用查询管理:内置24个实用查询,可以修改查询工具名字、地址、注释、颜色、排序等! 管理帐号设置:更改当前管理员帐号/密码 查看防注入记录,清空防注入记录 7.√赞助链接管理(方便站长卖链接):内置8个赞助链接,可以修改赞助链接名字、地址、位置(首页/内页)、到期时间、颜色、排序等! 8.网站分类管理:类别添加管理、类别删除管理、类别修改管理、类别拼音转换(重要!) 9.网址管理系统:添加网址链接、站长添加网站列表、用户添加网站列表、用户添加未审网站、有来路未审核网站、报错网站管理、黑名单网站管理、站内网站搜索 10.模版修改管理:分类页面模板、首页页面模板、关于本站页模板、联系本站页模板、使用帮助页模板、公益活动页模板、网址推荐页模板、友情连接页模板、常用软件页模板、竞价推荐页模板等修改/删除  11.生成静态页管理:生成分类页面、生成生成主页及其他页 12.数据库管理:备份数据库、恢复数据库、压缩数据库 13.全站样式与内容分类,标签调用 14.div布局,CSS控制样式,访问速度更快! 注:程序核心:晓宇网址导航系统
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值