In this video, we explore how to check if a given binary tree is a Binary Search Tree (BST) or not. A Binary Search Tree (BST) is a binary tree where the left subtree's nodes are smaller than the root, and the right subtree's nodes are greater, with the added condition that both subtrees must also be BSTs. We discuss three different approaches to solving this problem: using a specified range of minimum and maximum values, using inorder traversal, and using Morris Traversal. Each method ensures that the binary tree satisfies the properties of a BST, allowing you to verify if the tree is a BST or not.
The first approach uses a recursive helper function, isBSTUtil(), which checks whether a subtree is a BST within a given range. The second approach leverages inorder traversal, checking if the values of the nodes are sorted in ascending order. Finally, the third approach uses Morris Traversal, which eliminates the need for extra space while performing the check. This method temporarily alters the tree structure to create threads, helping maintain the BST properties while traversing the tree efficiently.
For more details, please go through - Check if a Binary Tree is BST or not