Check If the Binary Tree is Binary Search Tree or NotA binary search tree is a descendent of a more general binary tree with some constraints. In a binary search tree, the arrangement of nodes should follow certain properties. These properties are:
Each node's left and right subtree should also follow the two above properties of the Binary Search Tree. The Binary Search Tree does not contain any duplicate value. In this tutorial, we will see if the binary tree, through different ways, is a binary search tree or not. We will look at the various possible solutions. Approach - 1The most basic idea to solve this problem is to check for each node if the nodes in the left subtree are smaller than the parent node and the nodes in the right subtree are greater than the parent node. We will use a recursive function to solve this problem. Below is the algorithmic approach to solve the problem:
Below is the Python code of the above method. Code Output: The given tree is a BST Time Complexity: O(N2) is the time complexity of this method. The time complexity is non-linear because the primary function to check the conditions of the binary tree visits every node; hence, O(N) and the functions to return the minimum and the maximum values also visit every node taking O(N) time. Hence the final time complexity is O(N) * O(N) = O(N^2). Space Complexity: The space complexity is O(H). Here H symbolizes the height of the given binary tree. This extra space is taken by the recursion stack. Approach - 2This approach's idea is to create a function that will take the node, minimum, and maximum values. This function will traverse the tree from top to bottom, keeping track of the range in which the node value should be present. Each time the minimum value and the maximum value will be updated so that we can check if the binary tree follows the conditions of the binary search tree. This method will visit each node only once. The initial values of the min_val and max_val should be the minimum and maximum integer a node can possibly hold. This will be the range of the root node, as the root node can hold any value. After each iteration, the range will be narrowed accordingly for the left and right subtrees. We cannot use this method if the tree consists of duplicate elements having the min_val and the max_val. This is the algorithmic approach to solve the problem:-
Below is the implementation of the above approach: Code Output: The given tree is a BST Time Complexity: This approach has a linear time complexity i.e., O(N). Here N is the number of nodes present in the binary tree. Space Complexity: The program will take O(H). The space is required to store the recursive function stack. Using the In-order Traversal to Check if the Binary Tree is BST or NotThis approach will use the in-order traversal to determine whether the given binary tree is a Binary Search Tree. There is a property that the in-order traversal of a binary search tree follows. This property is that the in-order traversal list of the binary search tree is always in ascending order due to the arrangement of the nodes in the BST. So, we will perform the in-order traversal and then check whether the output is in ascending order. If the output is not in ascending order, the given binary tree is not a Binary Search Tree. Below is the algorithmic approach to solving the problem using the approach mentioned above:
Code Output: The given tree is a BST Time Complexity: This approach also has linear time complexity, i.e., O(N), Where N is the number of nodes in the tree Space Complexity: The program will take O(H). The space is required to store the recursive function stack. We can reduce the space complexity further by not creating the array. We can keep track of the last element and check whether the current node value is greater than the previous node value. If not, then we will stop the loop and return False. If the loop is complete, the binary tree is a binary search tree. Code Output: The given tree is a BST Using the Morris Traversal to Find if the Binary Tree is a Binary Search Tree or NotBelow is the algorithmic approach to solving the problem using the Morris Traversal
Below is the Python code of the approach mentioned above Code Output: The binary tree is a valid BST Next TopicCount the Number of Bracket Reversals |
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India