# 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>
• neha

There is a bug in this code. Tried submitting in leetcode and it failed the test cases. Fix is..

if(l.isBST && r.isBST && l.upper=node.val){

should have been
if(l.isBST && r.isBST && l.upper=node.val){

• SR

Why do you assign:

lower = Integer.MAX_VALUE;
upper = Integer.MIN_VALUE;

?????????????????

• Iram22

private static int largestBST(Node n){
if(n==null) return 0;
int l=largestBST(n.left);
int r=largestBST(n.right);
if(l==-1 || r==-1) return -1; //check both sides before returning false;
if(n.left!=null && n.valn.right.val) return -1;
max=Math.max(max,1+l+r);
return 1+l+r;
}

• 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)
{