333. Largest BST Subtree

333. Largest BST Subtree

Description

Given the root of a binary tree, find the largest subtree, which is also a Binary Search Tree (BST), where the largest means subtree has the largest number of nodes.

A Binary Search Tree (BST) is a tree in which all the nodes follow the below-mentioned properties:

  • The left subtree values are less than the value of their parent (root) node’s value.
  • The right subtree values are greater than the value of their parent (root) node’s value.

Note: A subtree must include all of its descendants.

Example 1:

1
2
3
Input: root = [10,5,15,1,8,null,7]
Output: 3
Explanation: The Largest BST Subtree in this case is the highlighted one. The return value is the subtree's size, which is 3.

Example 2:

1
2
Input: root = [4,2,7,2,3,5,null,2,null,null,null,null,null,1]
Output: 2

Constraints:

  • The number of nodes in the tree is in the range [0, 10^4].
  • -10^4 <= Node.val <= 10^4

Follow up: Can you figure out ways to solve it with O(n) time complexity?

Hints/Notes

  • N/A

Solution

Language: C++

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
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left),
* right(right) {}
* };
*/
class Solution {
public:
int res = 0;

int largestBSTSubtree(TreeNode* root) {
traverse(root);
return res;
}

// the return value: number of nodes, min, max
vector<int> traverse(TreeNode* root) {
if (!root) {
return {0, INT_MAX, INT_MIN};
}

int valid = true;

int minVal = root->val, maxVal = root->val, cur = 1;
if (root->left) {
vector<int> left = traverse(root->left);
if (root->val <= left[2]) {
valid = false;
}
minVal = left[1];
cur += left[0];
}

if (root->right) {
vector<int> right = traverse(root->right);
if (root->val >= right[1]) {
valid = false;
}
maxVal = right[2];
cur += right[0];
}

if (valid) {
res = max(res, cur);
return {cur, minVal, maxVal};
} else {
return {0, INT_MIN, INT_MAX};
}
}
};