0
0
DSA Javascriptprogramming~10 mins

Convert Sorted Array to Balanced BST in DSA Javascript - 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 Javascript
const mid = Math.[1]((start + end) / 2);
Drag options to blanks, or click blank then click option'
Aabs
Bfloor
Cround
Dceil
Attempts:
3 left
💡 Hint
Common Mistakes
Using Math.ceil causes index to go out of range.
Using Math.round can pick wrong middle for even length arrays.
2fill in blank
medium

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

DSA Javascript
const node = new TreeNode([1][mid]);
Drag options to blanks, or click blank then click option'
Aarr
Barray
Cnums
Dlist
Attempts:
3 left
💡 Hint
Common Mistakes
Using undefined variable names like 'arr' or 'list'.
3fill in blank
hard

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

DSA Javascript
node.left = buildBST(nums, start, [1] - 1);
Drag options to blanks, or click blank then click option'
Amid
Bstart
Cend
Dmid + 1
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'mid + 1' causes overlap with right subtree.
Using 'end' or 'start' incorrectly limits the range.
4fill in blank
hard

Fill both blanks to build the right subtree correctly.

DSA Javascript
node.right = buildBST(nums, [1], [2]);
Drag options to blanks, or click blank then click option'
Amid + 1
Bmid - 1
Cend
Dstart
Attempts:
3 left
💡 Hint
Common Mistakes
Using mid - 1 as start index for right subtree.
Using start as end index causes wrong range.
5fill in blank
hard

Fill all three blanks to complete the main function that calls the helper.

DSA Javascript
function sortedArrayToBST([1]) {
  function buildBST([2], start, end) {
    if (start > end) return null;
    const mid = Math.floor((start + end) / 2);
    const node = new TreeNode(nums[mid]);
    node.left = buildBST(nums, start, mid - 1);
    node.right = buildBST(nums, mid + 1, end);
    return node;
  }
  return buildBST([3], 0, nums.length - 1);
}
Drag options to blanks, or click blank then click option'
Anums
Darray
Attempts:
3 left
💡 Hint
Common Mistakes
Using different names like 'array' causes reference errors.