CheckBST[1] ___PTA
来源:互联网 发布:mac 查看路由表命令 编辑:程序博客网 时间:2024/06/17 08:27
Given a binary tree, you are supposed to tell if it is a binary search tree. If the answer is yes, try to find the KKK-th largest key, else try to find the height of the tree.
Format of function:
int CheckBST ( BinTree T, int K );
where BinTree
is defined as the following:
typedef struct TNode *BinTree;struct TNode{ int Key; BinTree Left; BinTree Right;};
The function CheckBST
is supposed to return the K
-th largest key if T
is a binary search tree; or if not, return the negative height ofT
(for example, if the height is 555, you must return −5-5−5).
Here the height of a leaf node is defined to be 1. T
is not empty and all its keys are positive integers. K
is positive and is never more than the total number of nodes in the tree.
Sample program of judge:
#include <stdio.h>#include <stdlib.h>typedef struct TNode *BinTree;struct TNode{ int Key; BinTree Left; BinTree Right;};BinTree BuildTree(); /* details omitted */int CheckBST ( BinTree T, int K );int main(){ BinTree T; int K, out; T = BuildTree(); scanf("%d", &K); out = CheckBST(T, K); if ( out < 0 ) printf("No. Height = %d\n", -out); else printf("Yes. Key = %d\n", out); return 0;}/* 你的代码将被嵌在这里 */
Sample Input 1: (for the following tree)
4
Sample Output 1:
Yes. Key = 5
Sample Input 2: (for the following tree)
3
Sample Output 2:
No. Height = 3
#include <stdio.h>
#include <stdlib.h>
typedef structTNode *BinTree;
struct TNode{
int Key;
BinTree Left;
BinTree Right;
};
BinTree BuildTree(); /* details omitted */
int CheckBST ( BinTree T,int K );
BinTree BuildTree(){
BinTree BT;
char temp=0;
scanf("%c",&temp);
if (temp !='#') {
BT =(BinTree)malloc(sizeof(structTNode));
BT->Key = temp;
BT->Left =BuildTree();
BT->Right =BuildTree();
}
else {
BT = NULL;
}
return BT;
}
int main()
{
BinTree T;
int K, out;
T = BuildTree();
scanf("%d", &K);
out = CheckBST(T, K);
if ( out <0 )
printf("No. Height = %d\n", -out);
else
printf("Yes. Key = %d\n", out);
return0;
}
/* 你的代码将被嵌在这里 */
int HeightOfTree( BinTree T);
int Max (int a,int b);
void InOrderTraversal( BinTree BT,int* Order);
int Order[1000];
int number;
int CheckBST ( BinTree T,int K ){
int i=1;
InOrderTraversal(T,Order);
//i = Judeg(T);
if (number==1) {
i = 1;
}
for (int j=0; j<number-1; j++) {
if (Order[j]>=Order[j+1]) {
i = 0;
break;
}
}
if (i ==0) {
return -1*HeightOfTree(T);
}
else{
returnOrder[number-K];
}
}
int Max (int a,int b){
return a>b?a:b;
}
int HeightOfTree( BinTree T){
if (T ==NULL) {
return0;
}
else
returnMax(HeightOfTree(T->Left),HeightOfTree(T->Right))+1;
}
void InOrderTraversal( BinTree BT,int* Order){
if (BT){
InOrderTraversal(BT->Left, Order);
Order[number] = BT->Key;
number++;
InOrderTraversal(BT->Right, Order);
}
}
- CheckBST[1] ___PTA
- CheckBST[1]
- week5 4-1 CheckBST[1]
- 1
- 1
- 1
- 1
- 1
- 1
- 1》
- 1
- 1
- 1
- 1
- (1)
- 1
- 1
- 1
- windows下安装jdk并配置jdk环境
- 数据挖掘十大经典算法(1) C4.5
- [project X] tiny210(s5pv210)上电启动流程(BL0-BL2)
- 同轴光的发光特征及其使用时的注意事项
- MATLAB 灰度线性变换
- CheckBST[1] ___PTA
- Android requestLayout 和 invalidata , postInvalidate 比较
- python 读取excel失败 可以转换成csv文件
- 网络协议
- 数据挖掘十大经典算法(2) The k-means algorithm
- hihoCoder #1014 : Trie树
- APP跳转
- 深度学习(Deep Learning) 学习资料
- JSP基本概念及语法