0
0
DSA Javascriptprogramming~10 mins

Convert Sorted Array to Balanced BST in DSA Javascript - 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 of balanced BST
Split the array by middle element to create root, then recursively build left and right subtrees from subarrays.
Execution Sample
DSA Javascript
function sortedArrayToBST(arr) {
  if (!arr.length) return null;
  const mid = Math.floor(arr.length / 2);
  const root = { val: arr[mid] };
  root.left = sortedArrayToBST(arr.slice(0, mid));
  root.right = sortedArrayToBST(arr.slice(mid + 1));
  return root;
}
This code converts a sorted array into a balanced binary search tree by recursively choosing the middle element as root.
Execution Table
StepOperationCurrent ArrayMiddle ElementNode CreatedLeft Subtree CallRight Subtree CallVisual State
1Start[1,2,3,4,5,6,7]4Node(4)[1,2,3][5,6,7]4 / \
2Left subtree root[1,2,3]2Node(2)[1][3] 4 / \ 2
3Left subtree left leaf[1]1Node(1)[][] 4 / \ 2 /
4Left subtree right leaf[3]3Node(3)[][] 4 / \ 2 3 /
5Right subtree root[5,6,7]6Node(6)[5][7] 4 / \ 2 6 / \ / \
6Right subtree left leaf[5]5Node(5)[][] 4 / \ 2 6 / \ / \ 1 3 5 7
7Right subtree right leaf[7]7Node(7)[][] 4 / \ 2 6 / \ / \ 1 3 5 7
8Return rootN/AN/AN/AN/AN/ABalanced BST complete
9ExitN/AN/AN/AN/AN/AAll nodes created, recursion ends
💡 All subarrays processed, recursion ends with balanced BST root returned
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 4After Step 5After Step 6After Step 7Final
arr[1,2,3,4,5,6,7][1,2,3,4,5,6,7][1,2,3][1][3][5,6,7][5][7]N/A
midN/A3100100N/A
root.valN/A42136574
left subtreeN/ApendingNode(2)Node(1)Node(3)CompleteCompleteCompleteComplete
right subtreeN/ApendingpendingpendingpendingNode(6)Node(5)Node(7)Complete
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 stays balanced by evenly splitting the array into left and right subtrees.
What happens when the subarray is empty?
When the subarray is empty (like in Step 3 and Step 4 calls with []), the function returns null, stopping recursion and creating leaf nodes.
How do left and right subtrees connect to the root?
After creating the root node (Step 1), recursive calls build left and right subtrees which are assigned to root.left and root.right respectively, as shown in Steps 2-7.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the middle element chosen at Step 5?
A6
B5
C7
D4
💡 Hint
Check the 'Middle Element' column at Step 5 in execution_table.
At which step does the recursion create the left leaf node with value 1?
AStep 2
BStep 3
CStep 4
DStep 6
💡 Hint
Look at the 'Node Created' column and 'Current Array' at Step 3.
If the input array was empty, what would the function return immediately?
AA node with value 0
BAn error
Cnull
DAn empty array
💡 Hint
Refer to the base case in the code sample where empty array returns null.
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 array returns null
- Result is height-balanced BST
Full Transcript
This concept converts a sorted array into a balanced binary search tree by choosing the middle element as the root node. The array is split into left and right halves recursively, building left and right subtrees. When the subarray is empty, recursion stops returning null, creating leaf nodes. This ensures the tree is balanced with minimal height. The code uses recursion and array slicing to build the tree step-by-step.