题目见这里
(分析) 分四步进行:
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)的更多相关文章
-
PAT 1099 Build A Binary Search Tree[BST性质]
1099 Build A Binary Search Tree(30 分) A Binary Search Tree (BST) is recursively defined as a binary ...
-
PAT 1099. Build A Binary Search Tree (树的中序,层序遍历)
A Binary Search Tree (BST) is recursively defined as a binary tree which has the following propertie ...
-
PAT甲级——1099 Build A Binary Search Tree (二叉搜索树)
本文同步发布在CSDN:https://blog.csdn.net/weixin_44385565/article/details/90701125 1099 Build A Binary Searc ...
-
pat 甲级 1099. Build A Binary Search Tree (30)
1099. Build A Binary Search Tree (30) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN ...
-
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 ...
-
PAT (Advanced Level) Practise - 1099. Build A Binary Search Tree (30)
http://www.patest.cn/contests/pat-a-practise/1099 A Binary Search Tree (BST) is recursively defined ...
-
PAT 甲级 1099 Build A Binary Search Tree
https://pintia.cn/problem-sets/994805342720868352/problems/994805367987355648 A Binary Search Tree ( ...
-
PAT Advanced 1099 Build A Binary Search Tree (30) [⼆叉查找树BST]
题目 A Binary Search Tree (BST) is recursively defined as a binary tree which has the following proper ...
-
1099. Build A Binary Search Tree (30)
A Binary Search Tree (BST) is recursively defined as a binary tree which has the following propertie ...
-
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 ...
随机推荐
-
JDBC-Oracle
例子: publicclassTestJdbc { public static void main(String[] args)throwsException { //程序入口,并抛出异常 Class ...
-
hdu 1849(Rabbit and Grass) 尼姆博弈
Rabbit and Grass Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
-
【JSP】JSP基础学习记录(一)—— 基础介绍以及3个编译指令
序: 从实现到现在一直是以.net为主,但偶尔也会参与一些其他语言的项目.最近需要对一个Java Web项目进行二次开发,一直没学习过JSP所以买了几本书自学试试.参考资料为<轻量级Java E ...
-
logging
#coding=utf8 import sys, logging logging.basicConfig(level=logging.INFO, forma ...
-
缓存 Cache
Controllers层 public class HomeController : Controller { // // GET: /Home/ // ...
-
在CI框架下执行存储的方法
我直接把代码摆在这里分享哈 <?php /** * * Created by JetBrains PhpStorm. * User: lsl * Date: 14-1-8 * Time: 下午2 ...
-
android 内存优化一
常见内存泄露原因 Context对象泄漏 1.如果一个类持有Context对象的强引用,就需要检查其生存周期是否比Context对象更长.否则就可能发生Context泄漏. 2.View持有其创建所在 ...
-
gcc __thread关键字
https://blog.csdn.net/xj178926426/article/details/54345449 EventLoop.cpp __thread EventLoop* t_loopI ...
-
根据字符串导入包使用-----importlib
import importlibo = importlib.import_module("xx.oo")s2 = "Person"the_class = get ...
-
Http协议基础知识
r equest POST https://re.csdn.net/csdnbi HTTP/1.1方法 url/uri 协议的版本号 1.1Host: re.csdn.netConnection: k ...