Find the node with minimum value in a Binary Search Tree

PROBLEM :

Find the node with minimum value in a Binary Search Tree

This is quite simple. Just traverse the node from root to left recursively until left is NULL. The node whose left is NULL is the node with minimum value.



For the above tree, we start with 20, then we move left 8, we keep on moving to left until we see NULL. Since left of 4 is NULL, 4 is the node with minimum value.


--------------------------------------------------------------------------------
SIMPLE c++ IMPLEMENTATION :
--------------------------------------------------------------------------------

/*  Structure of a Binary Tree
struct Node
{
    int data;
    struct Node* left, *right;
}; */

/* Function to get the maximum width of a binary tree*/

tree* getMin_tree(tree *root)
{
tree* temp ;
if(root->left==NULL)
return root ;

temp=root->left ;
while(temp->left!=NULL)
temp=temp->left ;
return temp ;
}


---------------------------------------------------------------------------------

Comments

Popular posts from this blog

Count ways to N'th Stair(Order does not matter)

Replace all ‘0’ with ‘5’ in an input Integer

Chocolate Distribution Problem

Remove characters from the first string which are present in the second string

Primality Test ( CodeChef Problem code: PRB01 )