LeetCode – Largest BST Subtree (Java)

Given a binary tree, find the largest subtree which is a Binary Search Tree (BST), where largest means subtree with largest number of nodes in it.

Java Solution

class Wrapper{
    int size;
    int lower, upper;
    boolean isBST;
 
    public Wrapper(){
        lower = Integer.MAX_VALUE;
        upper = Integer.MIN_VALUE;
        isBST = false;
        size = 0;
    }
} 
public class Solution {
    public int largestBSTSubtree(TreeNode root) {
        return helper(root).size;
    }
 
    public Wrapper helper(TreeNode node){
        Wrapper curr = new Wrapper();
 
        if(node == null){
            curr.isBST= true;
            return curr;
        }
 
        Wrapper l = helper(node.left);
        Wrapper r = helper(node.right);
 
        //current subtree's boundaries
        curr.lower = Math.min(node.val, l.lower);
        curr.upper = Math.max(node.val, r.upper);
 
        //check left and right subtrees are BST or not
        //check left's upper again current's value and right's lower against current's value
        if(l.isBST && r.isBST && l.upper<=node.val && r.lower>=node.val){
            curr.size = l.size+r.size+1;
            curr.isBST = true;
        }else{
            curr.size = Math.max(l.size, r.size);
            curr.isBST  = false;
        }
 
        return curr;
    }
}
Category >> Algorithms  
If you want someone to read your code, please put the code inside <pre><code> and </code></pre> tags. For example:
<pre><code> 
String foo = "bar";
</code></pre>
  • Divyanshu Singh

    This code isn’t working for 2 test-cases. And I don’t have access to those test-cases. Can somebody pls point out the error.


    // Following is the Binary Tree node structure
    /**************
    class BinaryTreeNode {
    public :
    T data;
    BinaryTreeNode *left;
    BinaryTreeNode *right;

    BinaryTreeNode(T data) {
    this -> data = data;
    left = NULL;
    right = NULL;
    }
    };
    ***************/

    #include
    using namespace std;

    bool isLeaf(BinaryTreeNode* root)
    {
    if(root->left||root->right)
    return false;
    return true;
    }

    pair helper(BinaryTreeNode* root)
    {
    if(root==NULL)
    {
    pair p(1,0);
    return p;
    }

    if(isLeaf(root))
    {
    pair p(1,1);
    return p;
    }

    pair l = helper(root->left);
    pair r = helper(root->right);
    int lb = l.first; int lh = l.second;
    int rb = r.first; int rh = r.second;
    int b,h,x=root->data;

    if(root->left&&root->right)
    {
    if(x>root->left->data&&xright->data)
    {
    b=1; h=max(lh,rh)+1;
    pair ans(b,h);
    return ans;
    }
    else
    {
    b=0; h=max(lh,rh);
    pair ans(b,h);
    return ans;
    }
    }
    else if(root->left&&!root->right)
    {
    if(x>root->left->data)
    {
    b=1; h=max(lh,rh)+1;
    pair ans(b,h);
    return ans;
    }
    else
    {
    b=0; h=max(lh,rh);
    pair ans(b,h);
    return ans;
    }
    }
    else if(!root->left&&root->right)
    {
    if(xright->data)
    {
    b=1; h=max(lh,rh)+1;
    pair ans(b,h);
    return ans;
    }
    else
    {
    b=0; h=max(lh,rh);
    pair ans(b,h);
    return ans;
    }
    }
    }

    int largestBSTSubtree(BinaryTreeNode *root)
    {
    // Write your code here
    pair ans = helper(root);
    return ans.second;
    }