0
0
DSA Goprogramming~10 mins

Convert Sorted Array to Balanced BST in DSA Go - Interactive Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to find the middle index of the array.

DSA Go
mid := (start + [1]) / 2
Drag options to blanks, or click blank then click option'
Aend
Blen(arr)
C0
Dstart
Attempts:
3 left
💡 Hint
Common Mistakes
Using len(arr) instead of end index.
Using start twice instead of end.
2fill in blank
medium

Complete the code to create a new tree node with the middle element.

DSA Go
node := &TreeNode{Val: [1]
Drag options to blanks, or click blank then click option'
Aarr[mid]
Barr[end]
Carr[start]
Darr[0]
Attempts:
3 left
💡 Hint
Common Mistakes
Using arr[start] or arr[end] instead of arr[mid].
Using arr[0] which is always the first element.
3fill in blank
hard

Fix the error in the recursive call to build the left subtree.

DSA Go
node.Left = sortedArrayToBST(arr, start, [1])
Drag options to blanks, or click blank then click option'
Astart + 1
Bmid
Cmid - 1
Dend
Attempts:
3 left
💡 Hint
Common Mistakes
Using mid as end index includes the middle element again.
Using end includes elements after mid.
4fill in blank
hard

Fill both blanks to build the right subtree correctly.

DSA Go
node.Right = sortedArrayToBST(arr, [1], [2])
Drag options to blanks, or click blank then click option'
Amid + 1
Bstart
Cend
Dmid
Attempts:
3 left
💡 Hint
Common Mistakes
Using mid as start includes middle element again.
Using start or mid incorrectly as end index.
5fill in blank
hard

Fill all three blanks to complete the base case and recursive call.

DSA Go
if [1] > [2] {
    return nil
}

mid := (start + end) / 2
node := &TreeNode{Val: arr[mid]}
node.Left = sortedArrayToBST(arr, start, [3])
return node
Drag options to blanks, or click blank then click option'
Astart
Bend
Cmid - 1
Dmid + 1
Attempts:
3 left
💡 Hint
Common Mistakes
Using start >= end in base case instead of >.
Using mid or mid + 1 for left subtree end index.