0
0
DSA C++programming~20 mins

Maximum Width of Binary Tree in DSA C++ - Practice Problems & Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Maximum Width Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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;
}
A2
B3
C4
D5
Attempts:
2 left
💡 Hint
Think about how the width is calculated including null nodes between the leftmost and rightmost nodes at each level.
🧠 Conceptual
intermediate
1: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?
ATo skip null nodes and only count existing nodes
BTo prevent integer overflow by normalizing indices to start from zero at each level
CTo count only the nodes that have children at the current level
DTo ensure the indices are always even numbers
Attempts:
2 left
💡 Hint
Consider what happens to indices as the tree depth increases.
🔧 Debug
advanced
2: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);
}
AInteger overflow error due to large indices without normalization
BNo error, code runs correctly and returns max width
CRuntime error due to accessing null pointer
DCompilation error due to missing header files
Attempts:
2 left
💡 Hint
Look at how indices are handled inside the loop.
🚀 Application
advanced
1: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?
AUse DFS and calculate width by counting leaf nodes only
BUse DFS and count only existing nodes at each level
CUse BFS but ignore null nodes and count only present nodes
DUse BFS with node indices normalized by subtracting the leftmost index at each level
Attempts:
2 left
💡 Hint
Think about how to include gaps caused by missing nodes.
Predict Output
expert
2: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;
}
A8
B4
C5
D6
Attempts:
2 left
💡 Hint
Consider the indexing of nodes including null gaps at the deepest level.