Challenge - 5 Problems
Balanced BST Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output 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 middle-element approach?
DSA C++
struct TreeNode {
int val;
TreeNode* left;
TreeNode* right;
TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
};
TreeNode* sortedArrayToBST(vector<int>& nums, int left, int right) {
if (left > right) return nullptr;
int mid = left + (right - left) / 2;
TreeNode* root = new TreeNode(nums[mid]);
root->left = sortedArrayToBST(nums, left, mid - 1);
root->right = sortedArrayToBST(nums, mid + 1, right);
return root;
}
void inorder(TreeNode* root, vector<int>& res) {
if (!root) return;
inorder(root->left, res);
res.push_back(root->val);
inorder(root->right, res);
}
int main() {
vector<int> nums = {1, 2, 3, 4, 5, 6, 7};
TreeNode* root = sortedArrayToBST(nums, 0, nums.size() - 1);
vector<int> result;
inorder(root, result);
for (int v : result) cout << v << " ";
return 0;
}Attempts:
2 left
💡 Hint
Inorder traversal of a BST always returns the sorted array.
✗ Incorrect
The inorder traversal of a binary search tree visits nodes in ascending order. Since the tree is built from a sorted array, the inorder traversal returns the original sorted array.
🧠 Conceptual
intermediate1:30remaining
Height of Balanced BST from Sorted Array
What is the height of a balanced BST created from a sorted array of size 15 using the middle-element approach?
Attempts:
2 left
💡 Hint
Height of balanced BST is about log2(n+1).
✗ Incorrect
A balanced BST built from a sorted array has height approximately log2(n+1). For n=15, log2(16) = 4.
🔧 Debug
advanced2:00remaining
Identify the Bug in BST Construction
What error will this code produce when converting a sorted array to a balanced BST?
DSA C++
TreeNode* sortedArrayToBST(vector<int>& nums, int left, int right) { if (left >= right) return nullptr; int mid = (left + right) / 2; TreeNode* root = new TreeNode(nums[mid]); root->left = sortedArrayToBST(nums, left, mid - 1); root->right = sortedArrayToBST(nums, mid + 1, right); return root; }
Attempts:
2 left
💡 Hint
Check the base case condition carefully.
✗ Incorrect
The base case 'if (left >= right)' returns nullptr even when left == right, so single element subarrays never create nodes, causing missing nodes in the tree.
❓ Predict Output
advanced2:00remaining
Output of Level Order Traversal of Balanced BST
What is the output of the level order traversal of the balanced BST created from the sorted array [10, 20, 30, 40, 50, 60, 70]?
DSA C++
struct TreeNode {
int val;
TreeNode* left;
TreeNode* right;
TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
};
TreeNode* sortedArrayToBST(vector<int>& nums, int left, int right) {
if (left > right) return nullptr;
int mid = left + (right - left) / 2;
TreeNode* root = new TreeNode(nums[mid]);
root->left = sortedArrayToBST(nums, left, mid - 1);
root->right = sortedArrayToBST(nums, mid + 1, right);
return root;
}
vector<int> levelOrder(TreeNode* root) {
vector<int> res;
if (!root) return res;
queue<TreeNode*> q;
q.push(root);
while (!q.empty()) {
TreeNode* node = q.front(); q.pop();
res.push_back(node->val);
if (node->left) q.push(node->left);
if (node->right) q.push(node->right);
}
return res;
}
int main() {
vector<int> nums = {10, 20, 30, 40, 50, 60, 70};
TreeNode* root = sortedArrayToBST(nums, 0, nums.size() - 1);
vector<int> result = levelOrder(root);
for (int v : result) cout << v << " ";
return 0;
}Attempts:
2 left
💡 Hint
Level order visits nodes level by level from root.
✗ Incorrect
The middle element 40 becomes root, left subtree root 20, right subtree root 60, then their children in order.
🧠 Conceptual
expert1:30remaining
Minimum Number of Nodes for Height 5 Balanced BST
What is the minimum number of nodes in a balanced BST of height 5 created from a sorted array?
Attempts:
2 left
💡 Hint
Minimum nodes in height h balanced BST is 2^h - 1.
✗ Incorrect
A perfectly balanced BST of height 5 has minimum nodes = 2^5 - 1 = 31.