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 following C# code that converts a sorted array to a balanced binary search tree (BST), what is the output of the inorder traversal of the BST?
DSA C#
using System; using System.Collections.Generic; class TreeNode { public int val; public TreeNode left, right; public TreeNode(int x) { val = x; left = right = null; } } class Program { static TreeNode SortedArrayToBST(int[] nums) { return Helper(nums, 0, nums.Length - 1); } static TreeNode Helper(int[] nums, int left, int right) { if (left > right) return null; int mid = left + (right - left) / 2; TreeNode node = new TreeNode(nums[mid]); node.left = Helper(nums, left, mid - 1); node.right = Helper(nums, mid + 1, right); return node; } static void Inorder(TreeNode root, List<int> result) { if (root == null) return; Inorder(root.left, result); result.Add(root.val); Inorder(root.right, result); } static void Main() { int[] sortedArray = {1, 2, 3, 4, 5, 6, 7}; TreeNode bstRoot = SortedArrayToBST(sortedArray); List<int> inorderResult = new List<int>(); Inorder(bstRoot, inorderResult); Console.WriteLine(string.Join(", ", inorderResult)); } }
Attempts:
2 left
💡 Hint
Inorder traversal of a BST always returns the elements in sorted order.
✗ Incorrect
The code builds a balanced BST by choosing the middle element as root recursively. Inorder traversal visits nodes in ascending order, so the output matches the original sorted array.
🧠 Conceptual
intermediateHeight of Balanced BST from Sorted Array
If you convert a sorted array of length 15 into a balanced binary search tree using the middle element as root recursively, what is the height of the resulting BST?
Attempts:
2 left
💡 Hint
Height of a balanced BST is approximately 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 if the base case in the helper function is changed from 'if (left > right) return null;' to 'if (left >= right) return null;' in the sorted array to BST conversion?
DSA C#
static TreeNode Helper(int[] nums, int left, int right) { if (left >= right) return null; int mid = left + (right - left) / 2; TreeNode node = new TreeNode(nums[mid]); node.left = Helper(nums, left, mid - 1); node.right = Helper(nums, mid + 1, right); return node; }
Attempts:
2 left
💡 Hint
Check what happens when left == right in the base case.
✗ Incorrect
Changing to 'left >= right' causes the function to skip creating nodes when left == right, missing leaf nodes and resulting in an incomplete BST.
❓ Predict Output
advancedOutput of Preorder Traversal of Balanced BST
What is the output of the preorder traversal of the balanced BST created from the sorted array [10, 20, 30, 40, 50] using the standard middle element method?
DSA C#
using System; using System.Collections.Generic; class TreeNode { public int val; public TreeNode left, right; public TreeNode(int x) { val = x; left = right = null; } } class Program { static TreeNode SortedArrayToBST(int[] nums) { return Helper(nums, 0, nums.Length - 1); } static TreeNode Helper(int[] nums, int left, int right) { if (left > right) return null; int mid = left + (right - left) / 2; TreeNode node = new TreeNode(nums[mid]); node.left = Helper(nums, left, mid - 1); node.right = Helper(nums, mid + 1, right); return node; } static void Preorder(TreeNode root, List<int> result) { if (root == null) return; result.Add(root.val); Preorder(root.left, result); Preorder(root.right, result); } static void Main() { int[] sortedArray = {10, 20, 30, 40, 50}; TreeNode bstRoot = SortedArrayToBST(sortedArray); List<int> preorderResult = new List<int>(); Preorder(bstRoot, preorderResult); Console.WriteLine(string.Join(", ", preorderResult)); } }
Attempts:
2 left
💡 Hint
Preorder traversal visits root, then left subtree, then right subtree.
✗ Incorrect
The middle element 30 is root, left subtree is [10,20], right subtree is [40,50]. Preorder visits root 30, then left subtree preorder (10,20), then right subtree preorder (40,50).
🧠 Conceptual
expertMinimum Number of Nodes in Balanced BST of Height 5
What is the minimum number of nodes in a balanced binary search tree of height 5?
Attempts:
2 left
💡 Hint
Use the formula for minimum nodes in AVL tree: N(h) = 1 + N(h-1) + N(h-2), with N(0)=1, N(1)=2.
✗ Incorrect
The minimum nodes in balanced BST of height h follows Fibonacci-like sequence. For height 5, minimum nodes = 16.
