Challenge - 5 Problems
Balanced BST Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of Balanced BST Inorder Traversal
Given the sorted array [1, 2, 3, 4, 5, 6, 7], what is the inorder traversal output of the balanced BST created by the standard Convert Sorted Array to Balanced BST algorithm?
DSA Java
class TreeNode { int val; TreeNode left, right; TreeNode(int x) { val = x; } } public class Solution { public TreeNode sortedArrayToBST(int[] nums) { return helper(nums, 0, nums.length - 1); } private TreeNode helper(int[] nums, int left, int right) { if (left > right) return null; int mid = left + (right - left) / 2; TreeNode root = new TreeNode(nums[mid]); root.left = helper(nums, left, mid - 1); root.right = helper(nums, mid + 1, right); return root; } public void inorder(TreeNode root) { if (root == null) return; inorder(root.left); System.out.print(root.val + " "); inorder(root.right); } public static void main(String[] args) { Solution sol = new Solution(); int[] arr = {1, 2, 3, 4, 5, 6, 7}; TreeNode root = sol.sortedArrayToBST(arr); sol.inorder(root); } }
Attempts:
2 left
💡 Hint
Inorder traversal of a BST always returns the nodes in sorted order.
✗ Incorrect
The inorder traversal of a BST visits nodes in ascending order. Since the input array is sorted and the BST is balanced, the inorder traversal outputs the original sorted array.
🧠 Conceptual
intermediateHeight of Balanced BST from Sorted Array
If you convert a sorted array of length 15 into a balanced BST using the standard method, what is the height of the resulting BST?
Attempts:
2 left
💡 Hint
Height of a balanced BST is about log2(n+1).
✗ Incorrect
A balanced BST built from 15 nodes has height floor(log2(15)) + 1 = 4.
🔧 Debug
advancedIdentify the Bug in BST Construction
What error will occur when running this code to convert a sorted array to a balanced BST?
DSA Java
public TreeNode sortedArrayToBST(int[] nums) { return helper(nums, 0, nums.length); } private TreeNode helper(int[] nums, int left, int right) { if (left > right) return null; int mid = (left + right) / 2; TreeNode root = new TreeNode(nums[mid]); root.left = helper(nums, left, mid - 1); root.right = helper(nums, mid + 1, right); return root; }
Attempts:
2 left
💡 Hint
Check the range used for recursion and array indexing.
✗ Incorrect
The initial call uses right = nums.length which is out of bounds for array indexing (max index is nums.length - 1). This causes ArrayIndexOutOfBoundsException.
🚀 Application
advancedNumber of Nodes in Left Subtree
For a sorted array of length 9 converted to a balanced BST, how many nodes are in the left subtree of the root?
Attempts:
2 left
💡 Hint
The root is the middle element; left subtree contains elements before it.
✗ Incorrect
For 9 elements, root is at index 4 (0-based). Left subtree has elements at indices 0 to 3, total 4 nodes.
📝 Syntax
expertCorrect Syntax for Recursive BST Construction
Which option correctly implements the recursive method to convert a sorted array to a balanced BST in Java?
Attempts:
2 left
💡 Hint
Check base case and correct indices for recursive calls.
✗ Incorrect
Option A correctly uses base case left > right, mid calculation, and recursive calls with mid-1 and mid+1 boundaries. Others have off-by-one errors or incorrect base cases.
