924. Largest BST Subtree

MediumTreesBSTTree DPDFS

Given the root of a binary tree, return the number of nodes in the largest subtree that is itself a valid binary search tree. A subtree includes a node and all its descendants. The tree is given as a level-order array.

Input: A level-order array of the tree.

Output: Integer — the size of the largest BST subtree.

Examples

Example 1
Input: [10,5,15,1,8,null,7]
Output: 3
Explanation: The subtree rooted at 5 (nodes 5,1,8) is a BST of size 3.
Example 2
Input: [2,1,3]
Output: 3
Explanation: The whole tree is a BST.
Example 3
Input: [1]
Output: 1
Explanation: Single node.

Constraints

Asked by

MicrosoftMeta
Solve this problem in the editor →