0
0
DSA Goprogramming~10 mins

Convert Sorted Array to Balanced BST in DSA Go - Execution Trace

Choose your learning style9 modes available
Concept Flow - Convert Sorted Array to Balanced BST
Start with sorted array
Find middle element
Create root node with middle
Left subarray
Recursively build left subtree
Right subarray
Recursively build right subtree
Attach left and right subtrees to root
Return root node of balanced BST
Split the sorted array by middle element to create root, then recursively build left and right subtrees from subarrays.
Execution Sample
DSA Go
func sortedArrayToBST(nums []int) *TreeNode {
  if len(nums) == 0 {
    return nil
  }
  mid := len(nums) / 2
  root := &TreeNode{Val: nums[mid]}
  root.Left = sortedArrayToBST(nums[:mid])
  root.Right = sortedArrayToBST(nums[mid+1:])
  return root
}
This function converts a sorted array into a balanced binary search tree by recursively choosing the middle element as root.
Execution Table
StepOperationSubarrayNode CreatedPointer ChangesVisual State
1Start with full array[1, 2, 3, 4, 5, 6, 7]nilnilEmpty tree
2Find middle[1, 2, 3, 4, 5, 6, 7]Node(4)root = Node(4)4
3Build left subtree[1, 2, 3]Node(2)root.Left = Node(2) 4 / 2
4Build left subtree of left[1]Node(1)Node(2).Left = Node(1) 4 / 2 / 1
5Build right subtree of left[3]Node(3)Node(2).Right = Node(3) 4 / 2 / \ 1 3
6Build right subtree[5, 6, 7]Node(6)root.Right = Node(6) 4 / \ 2 6 / \
7Build left subtree of right[5]Node(5)Node(6).Left = Node(5) 4 / \ 2 6 / \ / 1 3 5
8Build right subtree of right[7]Node(7)Node(6).Right = Node(7) 4 / \ 2 6 / \ / \ 1 3 5 7
9Return rootFull treenilnilBalanced BST complete
10EndNo more subarraysnilnilConstruction finished
💡 All subarrays processed, recursion ends, balanced BST constructed
Variable Tracker
VariableStartAfter Step 2After Step 3After Step 6Final
nums[1,2,3,4,5,6,7][1,2,3,4,5,6,7][1,2,3][5,6,7]N/A
midN/A311N/A
rootnilNode(4)Node(4)Node(4)Node(4)
root.LeftnilnilNode(2)Node(2)Node(2)
root.RightnilnilnilNode(6)Node(6)
Key Moments - 3 Insights
Why do we pick the middle element as root?
Choosing the middle element (see Step 2 in execution_table) ensures the tree stays balanced because it divides the array into two equal halves for left and right subtrees.
What happens when the subarray is empty?
When the subarray is empty (like after Step 9), the function returns nil, which means no node is created, stopping recursion on that branch.
How do left and right pointers get assigned?
After creating the root node, recursive calls build left and right subtrees (Steps 3-8), and their returned nodes are assigned to root.Left and root.Right respectively.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at Step 4, which node is created and where is it attached?
ANode(1) attached as left child of Node(2)
BNode(3) attached as right child of Node(4)
CNode(5) attached as left child of Node(6)
DNode(7) attached as right child of Node(6)
💡 Hint
Check Step 4 in execution_table under 'Node Created' and 'Pointer Changes'
At which step does the right subtree of the root start building?
AStep 3
BStep 6
CStep 8
DStep 2
💡 Hint
Look at the 'Operation' column in execution_table for building right subtree
If the input array was empty, what would the function return?
AAn error
BA node with value 0
Cnil (no node)
DA node with value from the first element
💡 Hint
Refer to Step 1 and the base case in the code sample where len(nums) == 0
Concept Snapshot
Convert Sorted Array to Balanced BST:
- Pick middle element as root
- Recursively build left subtree from left half
- Recursively build right subtree from right half
- Base case: empty subarray returns nil
- Result is a height-balanced BST
Full Transcript
This concept converts a sorted array into a balanced binary search tree by recursively selecting the middle element as the root node. The array is split into left and right halves, which become the left and right subtrees respectively. The recursion stops when the subarray is empty, returning nil. This approach ensures the tree is balanced because each root divides the array evenly. The execution table shows each step of node creation and pointer assignment, building the tree from bottom up. Key moments clarify why the middle element is chosen and how recursion stops. The visual quiz tests understanding of node placement and recursion steps.