Last active
September 3, 2017 19:29
-
-
Save cixuuz/53aa6fd9ab295799e371fb0a5fc82678 to your computer and use it in GitHub Desktop.
[333. Largest BST Subtree] #leetcode
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Solution { | |
class Result { // (size, rangeLower, rangeUpper) -- size of current tree, range of current tree [rangeLower, rangeUpper] | |
int size; | |
int lower; | |
int upper; | |
Result(int size, int lower, int upper) { | |
this.size = size; | |
this.lower = lower; | |
this.upper = upper; | |
} | |
} | |
int max = 0; | |
public int largestBSTSubtree(TreeNode root) { | |
if (root == null) { return 0; } | |
traverse(root); | |
return max; | |
} | |
private Result traverse(TreeNode root) { | |
if (root == null) { return new Result(0, Integer.MAX_VALUE, Integer.MIN_VALUE); } | |
Result left = traverse(root.left); | |
Result right = traverse(root.right); | |
if (left.size == -1 || right.size == -1 || root.val <= left.upper || root.val >= right.lower) { | |
return new Result(-1, 0, 0); | |
} | |
int size = left.size + 1 + right.size; | |
max = Math.max(size, max); | |
return new Result(size, Math.min(left.lower, root.val), Math.max(right.upper, root.val)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment