333 Largest BST Subtree
Given a binary tree, find the largest subtree which is a Binary Search Tree (BST), where largest means subtree with largest number of nodes in it.
Note: A subtree must include all of its descendants. Here's an example:
The Largest BST Subtree in this case is the highlighted one.
The return value is the subtree's size, which is 3.
Follow up: Can you figure out ways to solve it with O(n) time complexity?
Solution 1: Brute Force
The Idea: Traverse every subtree and check to see if it follows a valid BST. This is true if the in order traversal is ordered. If the tree is unordered, the traversal will terminate back up the call stack with the maximum set as INT_MIN
- as an intermediate sort of logic that will ensure the actually max will not update.
Complexity: O(n^2) time and O(n) space
Solution 2: Linear
**Note: Leetcode doesn't allow me to modify the TreeNode datastructure, as I have done here, but this is a working solution.
The Idea: We explore the children first, so our algorithm will follow a post-order traversal. A leaf node is always a valid BST, otherwise we check for that left < root < right
. So if it follows that the node is a BST, then we update the count of the root to be the sum of the count of it's left and right children. This process allows us accumulate the number of children through the tree. Initially the count for every node is set to 1 (as to be self-inclusive). As we accumulate these nodes, the absolute maximum number of valid BST nodes are updated. If the node is otherwise not in a valid place to be a BST, then the count is set to some really small number (working with a symbolic -infinity would be really nice). This will ensure that if the parent nodes are valid BST's but have invalid BST children, their count will not influence the abs_max
.
Complexity: O(N) time, O(N) space
Last updated