Minimum Element in Binary Search Tree

Java Implementation of finding minimum element in BST.

class Solution
{
    static class TreeNode{
        int data;
        TreeNode left,right;
        TreeNode(int data)
        {
            this.data=data;
        }
    }
    static int minValue(TreeNode node)
    {
        TreeNode temp=node;
        while(temp.left!=null)
        {
            temp=temp.left;
        }
        return temp.data;
    }
    
     public static void main(String[] args)
    {
        TreeNode root=new TreeNode(10);
        root.left=new TreeNode(5);
        root.right=new TreeNode(15);
        root.left.left=new TreeNode(4);
        root.left.right=new TreeNode(7);
        
        int min=minValue(root);
        System.out.println("Minimum Value in BST is "+min);
         
    }
    }

Output: Minimum Value in BST is 4

Time Complexity- log(n) for balanced tree and O(n) for skewed tree.

Space Complexity- O(1)

Leave a comment

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

Pin It on Pinterest