0
0
DSA C++programming~10 mins

Convert Sorted Array to Balanced BST in DSA C++ - 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
Balanced BST constructed
Split the sorted array by middle element to create root, then recursively build left and right subtrees from subarrays.
Execution Sample
DSA C++
TreeNode* sortedArrayToBST(vector<int>& nums, int left, int right) {
  if (left > right) return nullptr;
  int mid = left + (right - left) / 2;
  TreeNode* root = new TreeNode(nums[mid]);
  root->left = sortedArrayToBST(nums, left, mid - 1);
  root->right = sortedArrayToBST(nums, mid + 1, right);
  return root;
}
Recursively picks middle element as root and builds left and right subtrees from subarrays.
Execution Table
StepOperationSubarray RangeNode CreatedPointer ChangesVisual State
1Find middle of [0..6][0..6]Node(4)root = Node(4)4
2Build left subtree of 4[0..2]Node(2)root->left = Node(2) 4 / 2
3Build left subtree of 2[0..0]Node(1)Node(2)->left = Node(1) 4 / 2 / 1
4Build left subtree of 1[0..-1]NoneNode(1)->left = nullptrNo change
5Build right subtree of 1[1..0]NoneNode(1)->right = nullptrNo change
6Build right subtree of 2[2..2]Node(3)Node(2)->right = Node(3) 4 / 2 / \ 1 3
7Build left subtree of 3[2..1]NoneNode(3)->left = nullptrNo change
8Build right subtree of 3[3..2]NoneNode(3)->right = nullptrNo change
9Build right subtree of 4[4..6]Node(6)root->right = Node(6) 4 / \ 2 6 / \
10Build left subtree of 6[4..4]Node(5)Node(6)->left = Node(5) 4 / \ 2 6 / \ / 1 3 5
11Build left subtree of 5[4..3]NoneNode(5)->left = nullptrNo change
12Build right subtree of 5[5..4]NoneNode(5)->right = nullptrNo change
13Build right subtree of 6[6..6]Node(7)Node(6)->right = Node(7) 4 / \ 2 6 / \ / \ 1 3 5 7
14Build left subtree of 7[6..5]NoneNode(7)->left = nullptrNo change
15Build right subtree of 7[7..6]NoneNode(7)->right = nullptrNo change
16Return root nodeN/AN/AN/ABalanced BST complete
💡 Recursion ends when left > right, no nodes created for empty subarrays.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 6After Step 9After Step 13Final
left0000046N/A
right6620266N/A
midN/A310256N/A
rootnullptrNode(4)Node(4)Node(4)Node(4)Node(4)Node(4)Node(4)
current nodeN/ANode(4)Node(2)Node(1)Node(3)Node(6)Node(7)N/A
Key Moments - 3 Insights
Why do we pick the middle element as root?
Picking the middle element (see Step 1 in execution_table) ensures the tree is balanced because left and right subarrays have nearly equal sizes.
What happens when the subarray is empty?
When left > right (e.g., Step 4), recursion returns nullptr, meaning no node is created, stopping further branching.
How do left and right pointers get assigned?
After creating a node, recursive calls assign left and right children (see Steps 2, 6, 9, 13) by attaching returned nodes to pointers.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at Step 6, which node is created and attached?
ANode(3) attached as right child of Node(2)
BNode(5) attached as left child of Node(6)
CNode(1) attached as left child of Node(2)
DNode(7) attached as right child of Node(6)
💡 Hint
Check Step 6 row in execution_table under 'Node Created' and 'Pointer Changes'
At which step does the recursion stop for the left subtree of Node(1)?
AStep 5
BStep 4
CStep 7
DStep 8
💡 Hint
Look for subarray range where left > right and no node created in execution_table
If the input array had 5 elements instead of 7, how would the root node change?
ARoot would be the last element
BRoot would be the first element
CRoot would be the middle element of the 5 elements
DRoot would be the average of all elements
💡 Hint
Recall the concept_flow and Step 1 where middle element is chosen as root
Concept Snapshot
Convert Sorted Array to Balanced BST:
- Pick middle element as root
- Recursively build left subtree from left subarray
- Recursively build right subtree from right subarray
- Base case: empty subarray returns nullptr
- Result is 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 left half of the array forms the left subtree, and the right half forms the right subtree. The recursion stops when the subarray is empty. Each step creates nodes and attaches them as left or right children, ensuring the tree remains balanced. The execution table traces each recursive call, node creation, and pointer assignment, showing the tree's growth visually. Key moments clarify why the middle element is chosen, how recursion ends, and how pointers are assigned. The visual quiz tests understanding of node creation and recursion stopping points. The snapshot summarizes the process in simple steps.