Challenge - 5 Problems
Maximum Width Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of Maximum Width Calculation
What is the output of the following C++ code that calculates the maximum width of a binary tree?
DSA C++
struct TreeNode {
int val;
TreeNode* left;
TreeNode* right;
TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
};
#include <iostream>
#include <queue>
#include <algorithm>
int widthOfBinaryTree(TreeNode* root) {
if (!root) return 0;
unsigned long long maxWidth = 0;
std::queue<std::pair<TreeNode*, unsigned long long>> q;
q.push({root, 0});
while (!q.empty()) {
int size = q.size();
unsigned long long left = q.front().second;
unsigned long long right = left; // initialize right
for (int i = 0; i < size; i++) {
auto node = q.front().first;
unsigned long long idx = q.front().second - left;
q.pop();
right = idx;
if (node->left) q.push({node->left, 2 * idx + 1});
if (node->right) q.push({node->right, 2 * idx + 2});
}
maxWidth = std::max(maxWidth, right + 1);
}
return static_cast<int>(maxWidth);
}
int main() {
TreeNode* root = new TreeNode(1);
root->left = new TreeNode(3);
root->right = new TreeNode(2);
root->left->left = new TreeNode(5);
root->left->right = new TreeNode(3);
root->right->right = new TreeNode(9);
std::cout << widthOfBinaryTree(root) << std::endl;
return 0;
}Attempts:
2 left
💡 Hint
Think about how the width is calculated including null nodes between the leftmost and rightmost nodes at each level.
✗ Incorrect
The maximum width is calculated by indexing nodes as if the tree was a complete binary tree. The maximum width here is 4 at the last level (nodes 5, 3, null, 9).
🧠 Conceptual
intermediate1:30remaining
Understanding Indexing in Maximum Width Calculation
In the maximum width calculation of a binary tree, why do we subtract the leftmost index from all node indices at the current level before processing?
Attempts:
2 left
💡 Hint
Consider what happens to indices as the tree depth increases.
✗ Incorrect
Subtracting the leftmost index normalizes indices to start from zero at each level, preventing very large numbers that could cause overflow.
🔧 Debug
advanced2:00remaining
Identify the Bug in Width Calculation Code
What error will the following code produce when calculating the maximum width of a binary tree?
DSA C++
int widthOfBinaryTree(TreeNode* root) { if (!root) return 0; unsigned long long maxWidth = 0; std::queue<std::pair<TreeNode*, unsigned long long>> q; q.push({root, 0}); while (!q.empty()) { int size = q.size(); unsigned long long left = q.front().second; unsigned long long right = 0; for (int i = 0; i < size; i++) { auto node = q.front().first; unsigned long long idx = q.front().second; q.pop(); right = idx; if (node->left) q.push({node->left, 2 * idx + 1}); if (node->right) q.push({node->right, 2 * idx + 2}); } maxWidth = std::max(maxWidth, right - left + 1); } return static_cast<int>(maxWidth); }
Attempts:
2 left
💡 Hint
Look at how indices are handled inside the loop.
✗ Incorrect
Indices grow exponentially with tree depth. Without subtracting the leftmost index, they can become very large causing overflow.
🚀 Application
advanced1:30remaining
Maximum Width of a Sparse Binary Tree
Given a sparse binary tree where many nodes are missing, which approach correctly calculates the maximum width including null nodes between leftmost and rightmost nodes?
Attempts:
2 left
💡 Hint
Think about how to include gaps caused by missing nodes.
✗ Incorrect
BFS with normalized indices tracks positions including gaps, allowing correct width calculation even in sparse trees.
❓ Predict Output
expert2:30remaining
Output of Maximum Width for a Complex Tree
What is the output of the following code that calculates the maximum width of this binary tree?
DSA C++
struct TreeNode {
int val;
TreeNode* left;
TreeNode* right;
TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
};
#include <iostream>
#include <queue>
#include <algorithm>
int widthOfBinaryTree(TreeNode* root) {
if (!root) return 0;
unsigned long long maxWidth = 0;
std::queue<std::pair<TreeNode*, unsigned long long>> q;
q.push({root, 0});
while (!q.empty()) {
int size = q.size();
unsigned long long left = q.front().second;
unsigned long long right = left;
for (int i = 0; i < size; i++) {
auto node = q.front().first;
unsigned long long idx = q.front().second - left;
q.pop();
right = idx;
if (node->left) q.push({node->left, 2 * idx + 1});
if (node->right) q.push({node->right, 2 * idx + 2});
}
maxWidth = std::max(maxWidth, right + 1);
}
return static_cast<int>(maxWidth);
}
int main() {
TreeNode* root = new TreeNode(1);
root->left = new TreeNode(2);
root->right = new TreeNode(3);
root->left->left = new TreeNode(4);
root->right->right = new TreeNode(5);
root->left->left->left = new TreeNode(6);
root->right->right->right = new TreeNode(7);
std::cout << widthOfBinaryTree(root) << std::endl;
return 0;
}Attempts:
2 left
💡 Hint
Consider the indexing of nodes including null gaps at the deepest level.
✗ Incorrect
The maximum width is 8 at the last level, counting nodes 6, nulls, and 7 as if the tree was complete.