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>
```