Similar Trees
July 30, 2012
Level of node in a Binary Tree
July 31, 2012

Node with maximum value in a Binary search tree

Earlier, we have seen how to find the minimum element in a Binary Search Tree.
Write a function which will return the maximum value in a binary search tree. For example: For the below Binary search tree, the function should return 40
Structure of the Node of the Tree is as given below:

    struct Node
    {
        int data;
        Node* lptr; // Ptr to LEFT subtree
        Node* rptr; // ptr to RIGHT subtree
    };


Solution:

Function to get the maximum element in the tree will also be similar:

    int getMaximum(Node* root)
    {
        while(root->rptr != NULL)
            root = root->rptr;
        return root->data;
    }

Leave a Reply

Your email address will not be published. Required fields are marked *