Given a Binary Tree, write a function that returns the size of the largest subtree which is also a Binary Search Tree (BST). If the complete Binary Tree is BST, then return the size of the whole tree.
Input: 5 / \ 2 4 / \ 1 3 Output: 3 The following subtree is the maximum size BST subtree 2 / \ 1 3 Input: 50 / \ 30 60 / \ / \ 5 20 45 70 / \ 65 80 Output: 5 The following subtree is the maximum size BST subtree 60 / \ 45 70 / \ 65 80
We have discussed two methods in below post.
Find the largest BST subtree in a given Binary Tree | Set 1
In this post, a different O(n) solution is discussed. This solution is simpler than the solutions discussed above and works in O(n) time.
The idea is based on method 3 of check if a binary tree is BST article.
A Tree is BST if following is true for every node x.
- The largest value in left subtree (of x) is smaller than value of x.
- The smallest value in right subtree (of x) is greater than value of x.
We traverse tree in bottom up manner. For every traversed node, we return maximum and minimum values in subtree rooted with it. If any node follows above properties and size of
Size of largest BST is 2
Time Complexity : O(n)
This article is contributed by Shubham Gupta. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Find the largest BST subtree in a given Binary Tree | Set 1
- Maximum sub-tree sum in a Binary Tree such that the sub-tree is also a BST
- K'th Largest Element in BST when modification to BST is not allowed
- Two nodes of a BST are swapped, correct the BST | Set-2
- Complexity of different operations in Binary tree, Binary Search Tree and AVL tree
- Overview of Data Structures | Set 2 (Binary Tree, BST, Heap and Hash)
- Find k-th smallest element in BST (Order Statistics in BST)
- Two nodes of a BST are swapped, correct the BST
- Convert a normal BST to Balanced BST
- Check if a binary tree is subtree of another binary tree | Set 1
- Check if a binary tree is subtree of another binary tree | Set 2
- Convert a Binary Tree to Threaded binary tree | Set 1 (Using Queue)
- Convert a Binary Tree to Threaded binary tree | Set 2 (Efficient)
- Binary Tree | Set 3 (Types of Binary Tree)
- Binary Tree to Binary Search Tree Conversion using STL set
- A program to check if a binary tree is BST or not
- Convert a BST to a Binary Tree such that sum of all greater keys is added to every key
- Check if a Binary Tree (not BST) has duplicate values
- Build Binary Tree from BST such that it's level order traversal prints sorted data
- Floor in Binary Search Tree (BST)