【PAT甲级 - C++题解】1064 Complete Binary Search Tree

✍个人博客:https://blog.csdn.net/Newin2020?spm=1011.2415.3001.5343
📚专栏地址:PAT题解集合
📝原题地址:题目详情 - 1064 Complete Binary Search Tree (pintia.cn)
🔑中文翻译:完全二叉搜索树
📣专栏定位:为想考甲级PAT的小伙伴整理常考算法题解,祝大家都能取得满分!
❤️如果有收获的话,欢迎点赞👍收藏📁,您的支持就是我创作的最大动力💪

1064 Complete Binary Search Tree

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
题意

二叉搜索树 (BST) 递归定义为具有以下属性的二叉树:

  • 若它的左子树不空,则左子树上所有结点的值均小于它的根结点的值
  • 若它的右子树不空,则右子树上所有结点的值均大于或等于它的根结点的值
  • 它的左、右子树也分别为二叉搜索树

完全二叉树 (CBT) 定义为除最深层外的其他层的结点数都达到最大个数,最深层的所有结点都连续集中在最左边的二叉树。

现在,给定 N N N 个不同非负整数,表示 N N N 个结点的权值,用这 N N N 个结点可以构成唯一的完全二叉搜索树。

请你输出该完全二叉搜索树的层序遍历。

思路
  1. 我们可以利用完全二叉树的性质,用一个一维数组来存储,如果当前结点第下标为 u (假设下标从 1 开始),则满足以下条件:
    • 该结点的左孩子下标为 u * 2
    • 该结点的右孩子下标为 u * 2 + 1
  2. 由于题目给定的是结点的值,所以我们可以通过对结点的值进行排序,从而得到中序遍历的数组,因为二叉搜索树的中序遍历结果一定是有序的。
  3. 然后通过模拟中序遍历并利用上述完全二叉树的性质,将每一个值放到对应的位置上。
  4. 因为存储完全二叉树结点值的数组和其层序遍历的结果一样,直接从前往后输出该数组即可。

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-p0a4cRap-1664190867005)(PAT 甲级辅导.assets/5.png)]

代码
#include<bits/stdc++.h>
using namespace std;

const int N = 1010;
int n;
int w[N], tr[N];

//中序遍历
void dfs(int u, int& k)
{
    if (u * 2 <= n)  dfs(u * 2, k);
    tr[u] = w[k++];
    if (u * 2 + 1 <= n) dfs(u * 2 + 1, k);
}

int main()
{
    cin >> n;
    for (int i = 0; i < n; i++)    cin >> w[i];

    sort(w, w + n);    //得到中序遍历数组

    int k = 0;
    dfs(1, k);   //中序遍历的同时生成完全二叉树

    //输出结果
    cout << tr[1];
    for (int i = 2; i <= n; i++)   cout << " " << tr[i];
    cout << endl;

    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值