Largest BST Subtree

Given a binary tree. Find the size of largest subtree which is a Binary Search Tree (BST), where largest means subtree with the largest number of nodes in it.

Note:
A subtree must include all of its descendants.

For Example

Input 1:
                     10
                    / \
                   5  15
                  / \   \ 
                 1   8   7
Output 1:
    3
    Explanation 1:
        Largest BST subtree is
                            5
                           / \
                          1   8

Input 2:
                5
               / \
              3   8
             / \ / \
            1  4 7  9
Output 2:
     7
     Explanation 2:
        Given binary tree itself is BST.
NOTE: You only need to implement the given function. Do not read input, instead use the arguments to the function. Do not print the output, instead return values as specified. Still have a doubt? Checkout Sample Codes for more details.
Start solving Largest BST Subtree on Interview Code Editor
Sign Up
to access hints and editorial solutions for Largest BST Subtree

Discussion


Loading...
Click here to start solving coding interview questions