Javatpoint Logo
Javatpoint Logo

Largest BST in Binary Tree

Each child node in a binary tree consists of just two nodes (left and right). Data are merely represented by tree topologies. Specialized forms of Binary Trees (BSTs) that adhere to these criteria include

  • The left child node is smaller than its parent
  • Right child's parent node is larger than child node.

Consider the situation where we are asked to identify the largest binary search tree (BST) present in a binary tree.

In this work, we'll develop a method to identify the binary tree's greatest BST. It is possible to figure out the size of the complete binary tree when the binary tree is a BST.

Create a function that takes a Binary Tree as input and returns the size of the highest subtree, also referred to as a Binary Search Tree (BST). Return the overall tree's size if the entire binary tree is BST.

Instances:

Largest BST in Binary Tree

C Code:

Output:

Size of the largest BST is 2
  • Time Complexity: O(n)
  • Space complexity: O(n), because recursion has been used for the call stack.

Conclusion

In this lesson, we learnt what a binary tree and a binary search tree are, as well as how to use recursion to identify the largest BST in the provided binary tree. Recursion will be used to determine whether a subtree under each node is a BST or not, and the values will be returned accordingly.







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA