📖 Scenario: Imagine you have a sorted list of numbers representing book IDs in a library. You want to organize these IDs into a balanced tree structure so that searching for any book ID is fast and efficient.
🎯 Goal: Build a balanced Binary Search Tree (BST) from a sorted array of integers. The BST should be balanced, meaning the left and right subtrees of every node differ in height by no more than one.
📋 What You'll Learn
Create a sorted list called sorted_array with the exact values: [10, 20, 30, 40, 50, 60, 70]
Create a class called TreeNode with attributes val, left, and right
Write a function called sorted_array_to_bst that takes sorted_array and returns the root of a balanced BST
Print the BST nodes in in-order traversal to verify the tree structure
💡 Why This Matters
🌍 Real World
Balanced BSTs are used in databases and file systems to keep data sorted and allow fast search, insert, and delete operations.
💼 Career
Understanding how to build and traverse trees is essential for software engineers working with data structures, algorithms, and system design.
Progress0 / 4 steps
1
Create the sorted array
Create a list called sorted_array with these exact values: [10, 20, 30, 40, 50, 60, 70]
DSA Python
Hint
Use square brackets to create a list and separate numbers with commas.
2
Define the TreeNode class
Define a class called TreeNode with an __init__ method that takes val and sets self.val, self.left, and self.right to None
DSA Python
Hint
Define a class with class TreeNode: and an __init__ method that sets the value and initializes left and right children to None.
3
Write the function to convert sorted array to balanced BST
Write a function called sorted_array_to_bst that takes nums as input and returns the root of a balanced BST. Use recursion to pick the middle element as root, then build left and right subtrees from the left and right halves of the array.
DSA Python
Hint
Use the middle element as root, recursively build left subtree from left half, right subtree from right half.
4
Print the BST in in-order traversal
Write a function called inorder_traversal that takes root and prints the node values in in-order (left, root, right). Then call inorder_traversal with the BST created from sorted_array.
DSA Python
Hint
In-order traversal prints nodes in ascending order for BST. Use recursion to visit left child, print root, then right child.
Practice
(1/5)
1. What is the main goal when converting a sorted array to a balanced Binary Search Tree (BST)?
easy
A. To create a BST with minimal height
B. To create a linked list from the array
C. To reverse the array elements
D. To sort the array again
Solution
Step 1: Understand the problem goal
The goal is to convert a sorted array into a BST that is balanced, meaning the tree height is minimized for efficient operations.
Step 2: Reason about balanced BST
A balanced BST has roughly equal nodes on left and right subtrees, which reduces height and improves search time.
Final Answer:
To create a BST with minimal height -> Option A
Quick Check:
Balanced BST = minimal height [OK]
Hint: Pick option about minimal height balanced BST [OK]
Common Mistakes:
Thinking the array is reversed
Confusing BST with linked list
Ignoring balance and height
2. Which of the following Python function signatures is correct for converting a sorted array nums to a balanced BST using recursion?
easy
A. def sortedArrayToBST(nums: list) -> int:
B. def sortedArrayToBST(nums: int) -> TreeNode:
C. def sortedArrayToBST(nums: list[int]) -> Optional[TreeNode]:
D. def sortedArrayToBST(nums: list[int]) -> list[int]:
Solution
Step 1: Check parameter type
The input is a sorted array, so nums should be a list of integers: list[int].
Step 2: Check return type
The function returns the root of a BST, which is a TreeNode or None, so Optional[TreeNode] is correct.