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-55).

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);

    }

}


0 0
原创粉丝点击