-
Notifications
You must be signed in to change notification settings - Fork 0
/
LargestBSTSubtree.java
32 lines (30 loc) · 1016 Bytes
/
LargestBSTSubtree.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public int largestBSTSubtree(TreeNode root) {
if(root==null) return 0;
if(root.left ==null && root.right ==null) return 1;
if(isValid(root,null,null)) return countNodes(root);
return Math.max(largestBSTSubtree(root.left) , largestBSTSubtree(root.right));
}
private boolean isValid(TreeNode root,Integer min, Integer max)
{
if(root==null) return true;
if(min!=null && min>=root.val) return false;
if(max!=null && max<=root.val) return false;
return isValid(root.left,min,root.val) && isValid(root.right,root.val,max);
}
private int countNodes(TreeNode root)
{
if(root==null) return 0;
if(root.left==null && root.right==null) return 1;
return 1+countNodes(root.left) + countNodes(root.right);
}
}