Saturday 28 July 2012

How do you find out the fifth maximum element in an Binary Search Tree in efficient manner. Note :: You should not use use any extra space. i.e sorting Binary Search Tree and storing the results in an array and listing out the fifth element.



int numOfNode=0;
void maxNode(tree *rootNode)
{
        if(rootNode == NULL)
                return;
        maxNode(rootNode->right);
        numOfNode++;
        if(numOfNode == 5)
                printf("%d\n",rootNode->data);
        maxNode(rootNode->left);
}


No comments:

Post a Comment