-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathProblem_21.java
More file actions
103 lines (87 loc) · 3 KB
/
Copy pathProblem_21.java
File metadata and controls
103 lines (87 loc) · 3 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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
// Largest BST in a Binary Tree [VVVVV Imp]
class Problem_21 {
// A binary tree node
static class Node {
int data;
Node left, right;
Node(int item) {
data = item;
left = right = null;
}
}
Node root;
// Function to insert a new node with given key in BST
static Node insert(Node node, int key) {
// If the tree is empty, return a new node
if (node == null) {
return new Node(key);
}
// Otherwise, recur down the tree
if (key < node.data) {
node.left = insert(node.left, key);
} else {
node.right = insert(node.right, key);
}
return node;
}
// Function to find the size of the largest BST in a binary tree
static class Info {
int size; // Size of the subtree
int min; // Minimum value in the subtree
int max; // Maximum value in the subtree
int ans; // Size of the largest BST
boolean isBST; // Is the subtree a BST
Info(int size, int min, int max, int ans, boolean isBST) {
this.size = size;
this.min = min;
this.max = max;
this.ans = ans;
this.isBST = isBST;
}
}
static Info largestBSTUtil(Node node) {
// An empty tree is a BST of size 0
if (node == null) {
return new Info(0, Integer.MAX_VALUE, Integer.MIN_VALUE, 0, true);
}
// Leaf node is a BST of size 1
if (node.left == null && node.right == null) {
return new Info(1, node.data, node.data, 1, true);
}
// Recur for left and right subtrees
Info leftInfo = largestBSTUtil(node.left);
Info rightInfo = largestBSTUtil(node.right);
// Create a new Info for the current node
Info curr = new Info(0, 0, 0, 0, false);
curr.size = 1 + leftInfo.size + rightInfo.size;
// Check if the current subtree is a BST
if (leftInfo.isBST && rightInfo.isBST &&
leftInfo.max < node.data && rightInfo.min > node.data) {
curr.min = Math.min(leftInfo.min, Math.min(rightInfo.min, node.data));
curr.max = Math.max(rightInfo.max, Math.max(leftInfo.max, node.data));
curr.ans = curr.size;
curr.isBST = true;
return curr;
}
// If not a BST, return the maximum size of BST found in the subtrees
curr.ans = Math.max(leftInfo.ans, rightInfo.ans);
curr.isBST = false;
return curr;
}
static int largestBST(Node node) {
return largestBSTUtil(node).ans;
}
public static void main(String[] args) {
Node root = null;
root = insert(root, 50);
insert(root, 30);
insert(root, 60);
insert(root, 5);
insert(root, 20);
insert(root, 45);
insert(root, 70);
insert(root, 65);
insert(root, 80);
System.out.println("Size of the largest BST is " + largestBST(root));
}
}