How to check if a given Binary Tree is BST or not?

How to check if a given Binary Tree is BST or not?

For a binary tree to be a BST, it should follow the BST conditions at all nodes i.e.
root.val > root.left.val and root.val < root.right.val

You can read a full code implementation here.

There are several ways -

Method 1- Use in-order traversal and store the data in an array. Check whether the array is sorted or not

Time and Space complexity - O(n)

Method 2 - Take a variable and assign its value INT_MIN, use in order traversal if you find the value of left node is smaller than its root value then update the root with left node and for right node just check whether the value is greater or not.

Time complexity- O(n)

Space complexity- O(1)