You're given the root of a binary tree, and your mission is to find the largest subtree that is also a valid Binary Search Tree (BST).

But what makes this challenging? The "largest" means the subtree with the maximum number of nodes, and a subtree must include all of its descendants - you can't cherry-pick nodes!

BST Properties Reminder:

  • All nodes in the left subtree have values < root's value
  • All nodes in the right subtree have values > root's value
  • Both left and right subtrees are also BSTs

Goal: Return the size (number of nodes) of the largest valid BST subtree.

Example: In a tree like [10, 5, 15, 1, 8, null, 7], you need to identify which portions form valid BSTs and find the one with the most nodes.

Input & Output

example_1.py โ€” Mixed BST Tree
$ Input: root = [10,5,15,1,8,null,7]
โ€บ Output: 3
๐Ÿ’ก Note: The largest BST subtree is the one rooted at node 5, which includes nodes {5, 1, 8}. This subtree follows BST properties: 1 < 5 < 8. The right subtree rooted at 15 is invalid because 7 < 15 violates BST property.
example_2.py โ€” Single Node Tree
$ Input: root = [4,2,7,2,3,5,null,2,null,null,null,null,null,1]
โ€บ Output: 2
๐Ÿ’ก Note: The largest BST subtree has 2 nodes. Multiple subtrees of size 2 exist: {2,3} and others. The entire tree is not a BST due to duplicate values and structure violations.
example_3.py โ€” Complete BST Tree
$ Input: root = [2,1,3]
โ€บ Output: 3
๐Ÿ’ก Note: The entire tree is a valid BST since 1 < 2 < 3. Therefore, the largest BST subtree is the whole tree with 3 nodes.

Constraints

  • The number of nodes in the tree is in the range [0, 104]
  • -104 โ‰ค Node.val โ‰ค 104
  • A subtree must include all of its descendants

Visualization

Tap to expand
Floor 3: Room 10 (CENTER)Valid: Left(1-8) < 10 < Right(15) โŒ (Right has 7 < 10)Left Wing: Room 5Min: 1, Max: 8, Size: 3 โœ…Right Wing: Room 15Min: 7, Max: 15, Size: 2 โŒRoom 1Size: 1 โœ…Room 8Size: 1 โœ…Room 77 < 15 โŒ๐Ÿ“Š INSPECTION RESULTLargest Valid Section: Left Wing (Rooms 1, 5, 8)Size: 3 rooms โœ…
Understanding the Visualization
1
Start from the bottom floors
Begin inspection at leaf nodes (individual rooms) - each is automatically valid
2
Check floor connections
For each floor, verify that left wing rooms < center room < right wing rooms
3
Gather section information
Collect data: smallest room number, largest room number, total rooms, validity status
4
Report to upper management
Pass summary information upward so upper floors can make decisions efficiently
5
Track largest valid section
Keep record of the biggest properly organized section found during inspection
Key Takeaway
๐ŸŽฏ Key Insight: Bottom-up information gathering allows us to make BST validity decisions efficiently by passing (min, max, size, isBST) data from children to parents, avoiding redundant validations.
Asked in
Google 45 Amazon 38 Meta 32 Microsoft 28 Apple 22
78.0K Views
Medium-High Frequency
~18 min Avg. Time
2.2K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen