forked from Kraken-ops/Hacktoberfest-2022
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlargestBSTSubtree.java
More file actions
65 lines (57 loc) · 2.2 KB
/
Copy pathlargestBSTSubtree.java
File metadata and controls
65 lines (57 loc) · 2.2 KB
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
public class Solution {
/*
* BinaryTreeNode class
*
* class BinaryTreeNode<T>
* {
* T data;
* BinaryTreeNode<T> left;
* BinaryTreeNode<T> right;
* public BinaryTreeNode(T data)
* {
* this.data = data;
* }
* }
*/
public static int largestBSTSubtree(BinaryTreeNode<Integer> root) {
// Write your code here
return largestBSTSubtree1(root).second.second;
}
public static Pair<Pair<Integer, Integer>, Pair<Boolean, Integer>> largestBSTSubtree1(BinaryTreeNode<Integer> root)
{
if(root==null)
{
Pair<Pair<Integer, Integer>, Pair<Boolean, Integer>> output=new Pair<Pair<Integer, Integer>, Pair<Boolean, Integer>>();
output.first=new Pair<>();
output.first.first=Integer.MIN_VALUE;
output.first.second=Integer.MAX_VALUE;
output.second=new Pair<>();
output.second.first=true;
output.second.second=0;
return output;
}
Pair<Pair<Integer, Integer>, Pair<Boolean, Integer>> leftAnsPair=largestBSTSubtree1(root.left);
Pair<Pair<Integer, Integer>, Pair<Boolean, Integer>> rightAnsPair=largestBSTSubtree1(root.right);
Pair<Pair<Integer, Integer>, Pair<Boolean, Integer>> output=new Pair<Pair<Integer, Integer>, Pair<Boolean, Integer>>();
output.first=new Pair<>();
output.second=new Pair<>();
int max=Math.max(root.data, Math.max(leftAnsPair.first.first,rightAnsPair.first.first));
int min=Math.min(root.data, Math.min(leftAnsPair.first.second,rightAnsPair.first.second));
boolean isBST=(root.data>leftAnsPair.first.first) &&
(root.data<rightAnsPair.first.second) &&
leftAnsPair.second.first && rightAnsPair.second.first;
if(isBST)
output.second.second=Math.max(leftAnsPair.second.second, rightAnsPair.second.second)+1;
else
output.second.second=Math.max(leftAnsPair.second.second, rightAnsPair.second.second);
output.first.first=max;
output.first.second=min;
output.second.first=isBST;
return output;
}
}
class Pair<T, V>
{
T first;
V second;
}