Skip to content

Commit 22591d8

Browse files
committed
Largest BST
1 parent b6c3f8f commit 22591d8

File tree

2 files changed

+97
-1
lines changed

2 files changed

+97
-1
lines changed

DSA Crack Sheet/README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -221,10 +221,13 @@
221221
- []( "view question") - [Cpp Solution](./solutions/.cpp)
222222
- [Preorder to BST](https://practice.geeksforgeeks.org/problems/preorder-to-postorder4423/1# "view question") - [Cpp Solution](./solutions/Preorder%20to%20BST.cpp)
223223
- [Check whether BST contains Dead End](https://practice.geeksforgeeks.org/problems/check-whether-bst-contains-dead-end/1 "view question") - [Cpp Solution](./solutions/Check%20whether%20BST%20contains%20Dead%20End.cpp)
224-
- []( "view question") - [Cpp Solution](./solutions/.cpp)
224+
- [Flatten BST to sorted list](https://www.geeksforgeeks.org/flatten-bst-to-sorted-list-increasing-order/ "view post")
225+
- [Largest BST](https://practice.geeksforgeeks.org/problems/largest-bst/1# "view question") - [Cpp Solution](./solutions/Largest%20BST.cpp)
225226

226227
### Greedy Method
227228

229+
- []( "view question") - [Cpp Solution](./solutions/.cpp)
230+
228231
### Backtracking
229232

230233
- [Rat in a Maze Problem](https://practice.geeksforgeeks.org/problems/rat-in-a-maze-problem/1# "view question") - [Cpp Solution](./solutions/Rat%20in%20a%20Maze%20Problem.cpp)
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
/*
2+
Largest BST
3+
===========
4+
5+
Given a binary tree. Find the size of its largest subtree that is a Binary Search Tree.
6+
7+
Example 1:
8+
Input:
9+
1
10+
/ \
11+
4 4
12+
/ \
13+
6 8
14+
Output: 1
15+
Explanation: There's no sub-tree with size
16+
greater than 1 which forms a BST. All the
17+
leaf Nodes are the BSTs with size equal
18+
to 1.
19+
20+
Example 2:
21+
Input: 6 6 3 N 2 9 3 N 8 8 2
22+
6
23+
/ \
24+
6 3
25+
\ / \
26+
2 9 3
27+
\ / \
28+
8 8 2
29+
Output: 2
30+
Explanation: The following sub-tree is a
31+
BST of size 2:
32+
2
33+
/ \
34+
N 8
35+
Your Task:
36+
You don't need to read input or print anything. Your task is to complete the function largestBst() that takes the root node of the Binary Tree as its input and returns the size of the largest subtree which is also the BST. If the complete Binary Tree is a BST, return the size of the complete Binary Tree.
37+
38+
Expected Time Complexity: O(N).
39+
Expected Auxiliary Space: O(Height of the BST).
40+
41+
Constraints:
42+
1 <= Number of nodes <= 100000
43+
1 <= Data of a node <= 1000000
44+
*/
45+
46+
struct NodeCheck
47+
{
48+
bool isBST;
49+
int minNode, maxNode;
50+
int noOfNodes;
51+
};
52+
53+
NodeCheck dfs(Node *root, int &ans)
54+
{
55+
if (!root)
56+
{
57+
NodeCheck sub_ans;
58+
sub_ans.isBST = true;
59+
sub_ans.minNode = INT_MAX;
60+
sub_ans.maxNode = INT_MIN;
61+
sub_ans.noOfNodes = 0;
62+
return sub_ans;
63+
}
64+
65+
auto left = dfs(root->left, ans);
66+
auto right = dfs(root->right, ans);
67+
68+
int curr_count = 0;
69+
bool curr_bst = false;
70+
71+
if (left.isBST && right.isBST && left.maxNode < root->data && right.minNode > root->data)
72+
{
73+
curr_count = 1 + left.noOfNodes + right.noOfNodes;
74+
curr_bst = true;
75+
}
76+
77+
ans = max(ans, curr_count);
78+
79+
NodeCheck sub_ans;
80+
sub_ans.isBST = curr_bst;
81+
sub_ans.minNode = min(left.minNode, root->data);
82+
sub_ans.maxNode = max(right.maxNode, root->data);
83+
sub_ans.noOfNodes = curr_count;
84+
85+
return sub_ans;
86+
}
87+
88+
int largestBst(Node *root)
89+
{
90+
int ans = 0;
91+
dfs(root, ans);
92+
return ans;
93+
}

0 commit comments

Comments
 (0)