-
-
Notifications
You must be signed in to change notification settings - Fork 610
/
Copy pathLargestBSTSubtree.java
45 lines (36 loc) · 1006 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
33
34
35
36
37
38
39
40
41
42
43
44
45
package problems.medium;
import problems.utils.TreeNode;
import java.util.HashMap;
import java.util.Map;
/**
* Created by sherxon on 4/15/17.
*/
public class LargestBSTSubtree {
int max=0;
Map<TreeNode, Integer> map = new HashMap<>();
public int largestBSTSubtree(TreeNode root) {
if(root==null)return 0;
size(root);
go(root);
return max;
}
void go(TreeNode x){
if(x==null)return ;
if(check(x, Long.MIN_VALUE, Long.MAX_VALUE)){
max=Math.max(map.get(x), max);
}
go(x.left);
go(x.right);
}
boolean check(TreeNode x, long min, long max){
if(x==null)return true;
return x.val > min && x.val < max && check(x.left, min, x.val) && check(x.right, x.val, max);
}
int size(TreeNode x){
if(x==null)return 0;
if(map.containsKey(x))return map.get(x);
int size=size(x.left) + size(x.right)+1;
map.put(x, size);
return size;
}
}