PAT:1064. Complete Binary Search Tree (30) AC

#include<stdio.h>
#include<algorithm>
using namespace std;
const int MAX=1010;
int n;
int arr[MAX];      //存放原始数组
int arrI=0;
int CBT[MAX];      //二叉排序树层序遍历序列【思维】中序遍历在数组中存放的就是层序遍历序列

void inorder(int root)
{
  if(root>n)
    return;
  inorder(root*2);
  CBT[root]=arr[arrI++];
  inorder(root*2+1);
}

int main()
{
  scanf("%d",&n);
  for(int i=0 ; i<n ; ++i)
    scanf("%d",&arr[i]);
  sort(arr,arr+n);
  inorder(1);          //【caution】千万不能写成0,写成0的话*2永远都是0.牺牲CBT[0],从1开始
  for(int i=1 ; i<n ; ++i)
    printf("%d ",CBT[i]);
  printf("%d\n",CBT[n]);
  return 0;
}
时间: 2024-10-05 05:50:58

PAT:1064. Complete Binary Search Tree (30) AC的相关文章

PAT Advanced 1064 Complete Binary Search Tree (30分)

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 greate

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 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 tha

PAT Advanced Level 1064 Complete Binary Search Tree (30)(30 分)

1064 Complete Binary Search Tree (30)(30 分) 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

1064. Complete Binary Search Tree (30)

题目如下: 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

PAT (Advanced Level) 1064. Complete Binary Search Tree (30)

因为是要构造完全二叉树,所以树的形状已经确定了. 因此只要递归确定每个节点是多少即可. #include<cstdio> #include<cstring> #include<cmath> #include<queue> #include<vector> #include<string> #include<stack> #include<map> #include<algorithm> using

PAT甲题题解-1064. Complete Binary Search Tree (30)-中序和层次遍历,水

由于是满二叉树,用数组既可以表示父节点是i,则左孩子是2*i,右孩子是2*i+1另外根据二分搜索树的性质,中序遍历恰好是从小到大排序因此先中序遍历填充节点对应的值,然后再层次遍历输出即可. 又是一道遍历的水题... #include <iostream> #include <cstdio> #include <algorithm> #include <string.h> #include <queue> using namespace std;

pat(A) 1064. Complete Binary Search Tree(完全二叉树的中序建树)

代码: #include<cstdio> #include<algorithm> #define N 1005 using namespace std; int a[N]; int T[N]; int pos; int n; int cmp(int a,int b) { return a<b; } void Build(int i) { if(i>n) return; int l=i<<1; int r=l+1; Build(l); T[i]=a[pos++

pat1064. Complete Binary Search Tree (30)

1064. Complete Binary Search Tree (30) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue 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

1064 Complete Binary Search Tree (30 分)完全二叉树

1064 Complete Binary Search Tree (30 分) 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 n