Convert Sorted Array To Binary Search Tree

To solve this coding challenge, you need to convert a sorted array into a height-balanced binary search tree (BST). The key to achieving a height-balanced BST is to always select the middle element of the current segment of the array as the root of the subtree. This approach ensures that the tree remains balanced as the left and right subtrees will have approximately the same number of nodes. Now, let's break down the problem and explain the solution step-by-step.

Explanation

  1. Understanding the Problem :
    • You're given a sorted array, which is ordered in ascending fashion.
    • You need to convert this array into a height-balanced binary search tree (BST).
    • A height-balanced BST is one where the depths of the two subtrees of every node never differ by more than one.
    • The middle element of any sub-array will act as the root since it ensures even distribution of elements to the left and right subtrees.
  2. High-Level Solution Approach :
    • Use a recursive approach to build the tree.
    • Find the middle element of the current sublist (array slice) and make it the root node.
    • Recursively repeat the process for the left sublist (elements before the middle element) to build the left subtree.
    • Recursively apply the same process for the right sublist (elements after the middle element) to build the right subtree.
  3. Steps to Implement :
    • If the input list is empty, return
      None
      (base case).
    • Find the middle index of the list.
    • Create a tree node with the value at the middle index.
    • Recursively build the left subtree using the left part of the list.
    • Recursively build the right subtree using the right part of the list.
    • Return the constructed tree node (which serves as the root).

    Detailed Steps in Pseudocode

    Here we delve into the more granular steps of the algorithm with pseudocode:
  4. Base Case :
    • If the current sub-array (slice of array) is empty, return
      None
      .
  5. Recursive Case :
    • Calculate the middle index of the current sub-array.
    • Create a new tree node with the middle element's value.
    • Recursively create the left subtree by passing the left sub-array (elements before the middle index) to the same function.
    • Recursively create the right subtree by passing the right sub-array (elements after the middle index) to the same function.
    • Link these subtrees to the current node's left and right pointers respectively.
    • Return the current node as the root of the subtree.
Here is the pseudocode encapsulating the above logic:
                                            
# Define the TreeNode structure
# TreeNode:
#     value: int
#     left: TreeNode
#     right: TreeNode

# Function to convert sorted array to BST
FUNCTION sortedArrayToBST(nums):
    # Base case: if the array is empty
    IF nums IS EMPTY:
        RETURN None

    # Find the middle index of the array
    mid_index = LENGTH(nums) // 2
    
    # Create a new TreeNode with the middle element's value
    root_node = TreeNode(nums[mid_index])
    
    # Recursively build the left subtree with elements left of the middle
    root_node.left = sortedArrayToBST(nums[0:mid_index])
    
    # Recursively build the right subtree with elements right of the middle
    root_node.right = sortedArrayToBST(nums[mid_index+1:])
    
    # Return the root node of this subtree
    RETURN root_node

                                        
This pseudocode constructs a height-balanced BST by making sure every call to
sortedArrayToBST()
divides the input array into two halves, using the middle element as the root, thus ensuring the balance. This maintains the depth property of a BST so that no two leaves' depths differ by more than one. By consistently following this division, you ensure that the tree remains balanced and adheres to the BST properties.