PAT-1099(Build A Binary Search Tree)

时间:2022-11-15 17:12:32

  题目见这里

(分析) 分四步进行:

1)根据给定的结点情况建二叉树  2)对输入的键值排序(asending) 3)对二叉树中序遍历,同时对应赋key值 4)层次遍历(队列应用)

题目并不困难,但是我误入了trick,错误假定了结点按先序遍历是按顺序编号的(当然是受样例的影响),所以有了下面22分(满分30) 的submit(贴出来是因为这不失为好的巩固二叉树知识的程序)

#include <stdio.h>
#include <stdlib.h> //qsort,malloc
#define N 105 typedef struct node{
int data;
struct node *lChild,*rChild;
}BiNode,*BiTree; void CreatBiTree(BiTree *bt){
(*bt) = (BiTree)malloc(sizeof(BiNode));
(*bt)->lChild = (*bt)->rChild = NULL;
int left,right;
scanf("%d%d",&left,&right);
if(left!=-1) CreatBiTree(&((*bt)->lChild));
if(right!=-1) CreatBiTree(&((*bt)->rChild));
} int cmp(const void *a, const void *b){ //asending
return *(int *)a - *(int *)b;
} void Sort(int *key, int n){
int i;
for(i=0;i<n;i++)
scanf("%d",&key[i]);
qsort((void*)key,n,sizeof(key[0]),cmp);
} void LDR(BiTree bt, int key[]){
static int i = 0;
if(bt){
LDR(bt->lChild,key);
bt->data = key[i++];
LDR(bt->rChild,key);
}
} void LOT(BiTree bt){
BiTree q[N],bNode;
int front,rear,flag;
rear = front = flag = 0;
q[rear] = bt;
while(front<=rear){
bNode = q[front++];
if(!flag){
printf("%d",bNode->data);
flag = 1;
}
else printf(" %d",bNode->data);
if(bNode->lChild) q[++rear] = bNode->lChild;
if(bNode->rChild) q[++rear] = bNode->rChild;
}
printf("\n");
} void DestryBiTree(BiTree *bt){
if(*bt){
DestryBiTree(&((*bt)->lChild));
DestryBiTree(&((*bt)->rChild));
free(*bt);
}
} int main(){
int key[N];
BiTree bt;
int n;
// freopen("Data.txt","r",stdin);
scanf("%d",&n);
CreatBiTree(&bt,n);
Sort(key,n);
LDR(bt,key); //中序遍历
LOT(bt); //层次遍历
DestryBiTree(&bt);
return 0;
}

  当然,既然认识到这个错误,当然是因为找到了反例:

8
  7 1
  2 3
  -1 -1
  -1 4
  5 6
  -1 -1
  -1 -1
  -1 -1
  33 37 34 30 50 43 37 33

从上面的反例中,我们注意到创建链表形式的二叉树是不太可能的,而应采用数组形式,所以有了AC的提交:

#include <stdio.h>
#define N 105 typedef struct{
int lChild,rChild;
int data;
}Node; void CreatBiTree(Node node[], int n){
int i = 0;
for(;i<n;i++) scanf("%d%d",&node[i].lChild,&node[i].rChild);
} int cmp(const void *a, const void *b){ //asending
return *(int *)a - *(int *)b;
} void Sort(int *key, int n){
int i;
for(i=0;i<n;i++)
scanf("%d",&key[i]);
qsort((void*)key,n,sizeof(key[0]),cmp);
} void LDR(Node *node, int key[], int i){
static int j = 0;
if(node[i].lChild!=-1) LDR(node,key,node[i].lChild);
node[i].data = key[j++];
if(node[i].rChild!=-1) LDR(node,key,node[i].rChild);//注意不要写误
} void LOT(Node node[]){
Node q[N],qNode;
int front,rear;
rear = front = 0;
q[rear] = node[0];
while(front<=rear){
qNode = q[front++];
if(rear) printf(" ");
printf("%d",qNode.data);
if(qNode.lChild!=-1) q[++rear] = node[qNode.lChild];
if(qNode.rChild!=-1) q[++rear] = node[qNode.rChild];
}
printf("\n");
} int main(){
int key[N];
Node node[N];
int n;
// freopen("Data.txt","r",stdin);
scanf("%d",&n);
CreatBiTree(node,n);
Sort(key,n);
LDR(node,key,0); //中序遍历
LOT(node); //层次遍历
return 0;
}

PAT-1099(Build A Binary Search Tree)的更多相关文章

  1. PAT 1099 Build A Binary Search Tree&lbrack;BST性质&rsqb;

    1099 Build A Binary Search Tree(30 分) A Binary Search Tree (BST) is recursively defined as a binary ...

  2. PAT 1099&period; Build A Binary Search Tree &lpar;树的中序,层序遍历)

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

  3. PAT甲级——1099 Build A Binary Search Tree &lpar;二叉搜索树&rpar;

    本文同步发布在CSDN:https://blog.csdn.net/weixin_44385565/article/details/90701125 1099 Build A Binary Searc ...

  4. pat 甲级 1099&period; Build A Binary Search Tree &lpar;30&rpar;

    1099. Build A Binary Search Tree (30) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN ...

  5. 1099 Build A Binary Search Tree

    1099 Build A Binary Search Tree (30)(30 分) A Binary Search Tree (BST) is recursively defined as a bi ...

  6. PAT &lpar;Advanced Level&rpar; Practise - 1099&period; Build A Binary Search Tree &lpar;30&rpar;

    http://www.patest.cn/contests/pat-a-practise/1099 A Binary Search Tree (BST) is recursively defined ...

  7. PAT 甲级 1099 Build A Binary Search Tree

    https://pintia.cn/problem-sets/994805342720868352/problems/994805367987355648 A Binary Search Tree ( ...

  8. PAT Advanced 1099 Build A Binary Search Tree &lpar;30&rpar; &lbrack;⼆叉查找树BST&rsqb;

    题目 A Binary Search Tree (BST) is recursively defined as a binary tree which has the following proper ...

  9. 1099&period; Build A Binary Search Tree &lpar;30&rpar;

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

  10. PAT A1099 Build A Binary Search Tree (30 分)——二叉搜索树,中序遍历,层序遍历

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

随机推荐

  1. JDBC-Oracle

    例子: publicclassTestJdbc { public static void main(String[] args)throwsException { //程序入口,并抛出异常 Class ...

  2. hdu 1849(Rabbit and Grass) 尼姆博弈

    Rabbit and Grass Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  3. 【JSP】JSP基础学习记录(一)—— 基础介绍以及3个编译指令

    序: 从实现到现在一直是以.net为主,但偶尔也会参与一些其他语言的项目.最近需要对一个Java Web项目进行二次开发,一直没学习过JSP所以买了几本书自学试试.参考资料为<轻量级Java E ...

  4. logging

    #coding=utf8 import sys, logging   logging.basicConfig(level=logging.INFO,                     forma ...

  5. 缓存 Cache

    Controllers层 public class HomeController : Controller    {        //        // GET: /Home/       // ...

  6. 在CI框架下执行存储的方法

    我直接把代码摆在这里分享哈 <?php /** * * Created by JetBrains PhpStorm. * User: lsl * Date: 14-1-8 * Time: 下午2 ...

  7. android 内存优化一

    常见内存泄露原因 Context对象泄漏 1.如果一个类持有Context对象的强引用,就需要检查其生存周期是否比Context对象更长.否则就可能发生Context泄漏. 2.View持有其创建所在 ...

  8. gcc &lowbar;&lowbar;thread关键字

    https://blog.csdn.net/xj178926426/article/details/54345449 EventLoop.cpp __thread EventLoop* t_loopI ...

  9. 根据字符串导入包使用-----importlib

    import importlibo = importlib.import_module("xx.oo")s2 = "Person"the_class = get ...

  10. Http协议基础知识

    r equest POST https://re.csdn.net/csdnbi HTTP/1.1方法 url/uri 协议的版本号 1.1Host: re.csdn.netConnection: k ...