Check given array of size n can represent BST of n levels or notProblem Statement: You're given an array of size n and you need to determine if the elements in the array can be used to construct a Binary Search Tree (BST) with exactly n levels. The construction follows a specific rule for arranging elements in a tree. Let's consider a number X;
Note: Before insertion of number, we never go beyond a number already visited.Example 1: Input: 500, 200, 90, 250, 100 The given array represents the following elements in a BST (assuming that the left child is less than the parent, and the right child is greater than the parent): In this case, the BST has 4 levels, not 5 as indicated by the number of elements in the array. Therefore, the output is "No". Input: 5123, 3300, 783, 1111, 890 The given array represents the following elements in a BST: In this case, the BST has 4 levels, matching the number of elements in the array. Therefore, the output is "Yes". Approach 1: Building a BST Representation The method involves populating a tree structure by inserting the elements from the given array in a specific order that mimics a binary search tree (BST). Each element is placed according to whether it is less or greater than the preceding element. Once the tree is constructed, a check is performed to determine if the resulting structure is a valid binary search tree or not. Algorithm: Java Implementation of the above approach Time Complexity: The time complexity of constructing the binary tree is O(n), where n is the number of elements in the array. This is because we traverse each element in the array once to build the tree. The time complexity of checking if the constructed tree is a valid BST (using inorder traversal) is also O(n), as we visit each node once. Overall, the time complexity is O(n) due to the dominant factor being the tree construction and validation. Space Complexity: The space complexity for creating the tree is O(n) in the worst case. This is because we need to store all n elements in the array in the tree. The space complexity for the recursive stack during BST validation is O(h), where h is the height of the tree. In a balanced BST, the height h is O(log n), but in the worst case (skewed tree), it can be O(n). Therefore, the overall space complexity is O(max(n, h)). In the worst case, it can be O(n) for a skewed tree. Approach 2: Using Array
By applying these conditions, you can effectively determine the boundaries for left and right subtrees while traversing the array Java Implementation of the above approach Time Complexity: The time complexity of this code is O(n) in the worst case, where 'n' is the number of elements in the 'values' array. This is because it iterates through the entire array once, performing constant-time operations for each element. Space Complexity: The space complexity of this code is O(1), which means it uses a constant amount of memory regardless of the input size. It only uses a few integer variables ('max', 'min', 'n', 'i', 'isBST') and does not require additional memory that scales with the input size. Next TopicConstruct a linked list from 2D matrix |