CLOSE
🛠️ Settings

Problem Statement

Given a root of Binary Tree, where the nodes have integer values. Return the size of the largest subtree of the binary tree which is also a BST.

A binary search tree (BST) is a binary tree data structure which has the following properties.

  • The left subtree of a node contains only nodes with data less than the node’s data.
  • The right subtree of a node contains only nodes with data greater than the node’s data.
  • Both the left and right subtrees must also be binary search trees.

Examples

Example 1:

Input: root = [2, 1, 3]
Output: 3
Example 2:

Input: [10, null, 20, null, 30, null, 40, null, 50]
Output: 5

Different Approaches