Commonly asked Data Structures and Algorithms Problems by big tech and different solution approaches with code in Java and C

Powered by Blogger.

Wednesday, September 13, 2017

Largest BST in Binary Tree


Code:

 public boolean isLeaf(Node root)
    {
        if(root.left!=null && root.right!=null)
        return true;
        return false;
    }
    public int Bst(Node root,int max)
    {
        if(root==null || !isLeaf(root))
        return max;
        if(root.left.data>root.data || root.right.data<root.data)
        return max;
        return Math.max(Bst(root.left,max+2),Bst(root.right,max+2));
    }
    public int largestBst(Node node)
    {
        return Math.max(Bst(node.left,1),Bst(node.right,1));
    }

0 Comments:

Post a Comment

Stats