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++];
    Build(r);
}

int main()
{
    while(scanf("%d",&n)==1)
    {
        for(int i=0; i<n; i++)
            scanf("%d",&a[i]);
        sort(a,a+n,cmp);
        pos=0;
        Build(1);
        for(int i=1; i<=n; i++)
        {
            if(i==n)
                printf("%d\n",T[i]);
            else
                printf("%d ",T[i]);
        }
    }
    return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-11-07 17:23:21

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

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

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[roo

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

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 1064 Complete Binary Search Tree

1 #include <iostream> 2 #include <cstdio> 3 #include <cstdlib> 4 #include <vector> 5 #include <algorithm> 6 7 using namespace std; 8 9 class Node { 10 public: 11 int val; 12 Node* left; 13 Node* right; 14 public: 15 Node(): v