Largest BST Subtree Problem
Description
LeetCode Problem 333.
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:

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:
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?
Solution
Approach: Post-Order Traversal with Min, Max and Size
Checking every subtree separately for the BST property costs O(n) each, so O(n2) in total. Instead, let each subtree report what its parent needs, computed bottom-up in one post-order pass.
For a node, dfs(node) returns three values: the smallest value, the largest value and the size of the subtree, if the subtree is a BST. The subtree rooted at node is a BST exactly when:
- both children's subtrees are BSTs, and
node.valis greater than the largest value on the left and smaller than the smallest value on the right.
If so, its size is left size + right size + 1, its minimum is min(left min, node.val) and its maximum is max(right max, node.val). Update the best size seen.
Two sentinel values make the edge cases work without special code:
- An empty subtree returns
min = +∞, max = -∞, size = 0, so a missing child never blocks the check (-∞ < node.val < +∞). - A subtree that is not a BST returns
min = -∞, max = +∞. Every ancestor then fails the check, which is right: a subtree must include all its descendants, so nothing above a broken subtree can be a BST.
Values must be strictly ordered. In example 2, the subtree rooted at the lower 2 has another 2 as its left child, so it is not a BST, and neither is anything above it on the left side. The largest BSTs are [2, 1] (the inner 2 with its child 1) and [7, 5], both of size 2.
Complexity: O(n) time, since each node is visited once, and O(h) extra space for the recursion, where h is the height of the tree.
class Solution:
def largestBSTSubtree(self, root: Optional[TreeNode]) -> int:
inf = float("inf")
best = 0
# Returns (min, max, size) of the subtree if it is a BST, else (-inf, inf, 0):
# those bounds make every ancestor fail the BST check too.
def dfs(node: Optional[TreeNode]):
nonlocal best
if node is None:
return inf, -inf, 0 # an empty tree fits under any parent
lmin, lmax, lsize = dfs(node.left)
rmin, rmax, rsize = dfs(node.right)
if lmax < node.val < rmin:
size = lsize + rsize + 1
best = max(best, size)
return min(lmin, node.val), max(rmax, node.val), size
return -inf, inf, 0
dfs(root)
return best