Check given array of size n can represent BST of n levels or not

Problem 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;

  • Number greater than X is on the right side
  • Number smaller than X is on the left side.

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

  • Utilize two variables, 'max' initialized to INT_MAX to denote the upper limit for the left subtree, and 'min' initialized to INT_MIN to denote the lower limit for the right subtree. Iterate through the array from arr[1] to arr[n-1].
  • For each element in the array, perform the following checks:
  • If the current element (arr[i]) is greater than the previous element (arr[i-1]) and falls within the range between 'min' and 'max', update 'min' with the value of arr[i-1].
  • If the current element is not greater than the previous element but still falls within the range between 'min' and 'max', update 'max' with the value of the current element.
  • If neither of the above two conditions holds true, it implies that the current element cannot be inserted into a new level, and therefore, break out of the loop.

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.