0
0
DSA C++programming~10 mins

Convert Sorted Array to Balanced BST in DSA C++ - 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 segment.

DSA C++
int mid = start + (end - start) / [1];
Drag options to blanks, or click blank then click option'
A1
B3
C4
D2
Attempts:
3 left
💡 Hint
Common Mistakes
Using division by 3 or 4 instead of 2.
Forgetting to subtract start from end before dividing.
2fill in blank
medium

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

DSA C++
TreeNode* node = new TreeNode([1][mid]);
Drag options to blanks, or click blank then click option'
Alist
Bvector
Carr
Dtree
Attempts:
3 left
💡 Hint
Common Mistakes
Using an undefined variable name like 'list' or 'vector'.
Using 'tree' which is unrelated.
3fill in blank
hard

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

DSA C++
node->left = buildBST(arr, 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 start or end incorrectly as the end index.
Using mid + 1 which belongs to the right subtree.
4fill in blank
hard

Fill both blanks to build the right subtree correctly.

DSA C++
node->right = buildBST(arr, [1], [2]);
Drag options to blanks, or click blank then click option'
Amid + 1
Bstart
Cend
Dmid - 1
Attempts:
3 left
💡 Hint
Common Mistakes
Using start or mid - 1 as start index for right subtree.
Swapping start and end indices.
5fill in blank
hard

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

DSA C++
if (start > [1]) return nullptr;
int mid = start + (end - start) / [2];
TreeNode* node = new TreeNode(arr[mid]);
node->left = buildBST(arr, start, mid - 1);
node->right = buildBST(arr, mid + 1, [3]);
return node;
Drag options to blanks, or click blank then click option'
Aend
B2
Dstart
Attempts:
3 left
💡 Hint
Common Mistakes
Using start instead of end in base case.
Dividing by wrong number in mid calculation.
Using wrong end index for right subtree.