【1064】Complete Binary Search Tree (30 分)

简介: 【1064】Complete Binary Search Tree (30 分)【1064】Complete Binary Search Tree (30 分)
#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include<string>
#include<algorithm>  
#include<map>
#include<vector>
#include<queue> 
using namespace std;  
const int maxn=1010;
//n为结点数,number用以存放结点权值,CBT用以存放完全二叉树
//index从小到大枚举number数组
int n,number[maxn],CBT[maxn];
int index=0; //index如果在string.h头文件下会报错重定义(要改名)
//或者将string.h头文件直接改成string头文件
void inOrder(int root){  //中序遍历
  if(root >n)  return ;//空结点,直接返回
  inOrder(root*2);  //往左子树递归
  CBT[root]=number[index++];  //根结点处赋值number[index]
  inOrder(root*2+1);  //往右子树递归
}
int main(){   
  scanf("%d",&n);
  for(int i=0;i<n;i++){
    scanf("%d",&number[i]);
  }
  sort(number,number+n);  //从小到大排序
  inOrder(1);  //1号位为根结点
  for(int i=1;i<=n;i++){
    printf("%d",CBT[i]);  //CBT数组本身就是层序
    if(i<n)  printf(" ");
  }
  system("pause"); 
    return 0;   
}
相关文章
|
6月前
|
算法 索引
Binary Search
Binary Search “【5月更文挑战第21天】”
49 5
|
6月前
C. Binary Search
C. Binary Search
|
机器学习/深度学习 存储 C++
【PAT甲级 - C++题解】1064 Complete Binary Search Tree
【PAT甲级 - C++题解】1064 Complete Binary Search Tree
90 0
LeetCode 222. Count Complete Tree Nodes
给出一个完全二叉树,求出该树的节点个数。
90 0
LeetCode 222. Count Complete Tree Nodes
PAT (Advanced Level) Practice - 1043 Is It a Binary Search Tree(25 分)
PAT (Advanced Level) Practice - 1043 Is It a Binary Search Tree(25 分)
123 0
|
存储 算法
PAT (Advanced Level) Practice - 1151 LCA in a Binary Tree(30 分)
PAT (Advanced Level) Practice - 1151 LCA in a Binary Tree(30 分)
127 0
|
算法 Python
Leetcode-Medium 98. Validate Binary Search Tree
Leetcode-Medium 98. Validate Binary Search Tree
127 0
【1043】Is It a Binary Search Tree (25 分)
【1043】Is It a Binary Search Tree (25 分) 【1043】Is It a Binary Search Tree (25 分)
121 0
【1099】Build A Binary Search Tree (30 分)
【1099】Build A Binary Search Tree (30 分) 【1099】Build A Binary Search Tree (30 分)
117 0
1110. Complete Binary Tree (25)
#include #include #include #include using namespace std; struct node { int ad, l, r; }; vector visited(20, ...
985 0