Minimum and Maximum element in BST

//If the given root node is empty, the return -1, else move to the left and left and left-est child of the root, which will be the minimum value

//Similarly, for maximum value, move to the rightest child for every node, until last right node is achieved,
which will be the value in the BST.


int minValue(Node* root)
{
    if(root==0)
    return -1;
    else if (root->left==0)
    return root->data;
    else
    return minValue(root->left);
}

int maxValue(Node* root)
{
    if(root==0)
    return -1;
    else if (root->right==0)
    return root->data;
    else
    return maxValue(root->right);
}

Comments

Popular posts from this blog

First_Come_First_Serve CPU Scheduling

Reversing stack Method 2 !! (One Helper Stack only)

Populating Next Right Pointers in Each Node in O(1) space (without queue and level order)

Calculate factorial of large numbers !! (Using Arrays)

Multiplication of large numbers (Given in string format)

Left View of Binary Tree (Method 1 using recursion)

Check Bracket Sequence

Image Multiplication

Boundary Traversal of binary tree

BST to greater sum tree