Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to declare the function that calculates the maximum width of a binary tree.
DSA C++
int [1](TreeNode* root); Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using maxDepth or maxHeight which relate to tree height, not width.
Using generic names like maxSize that don't specify width.
✗ Incorrect
The function name should be maxWidth as it clearly indicates the purpose of calculating the maximum width of the binary tree.
2fill in blank
mediumComplete the code to initialize the queue for level order traversal.
DSA C++
std::queue<std::pair<TreeNode*, unsigned long long>> [1]; Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'stack' which is a different data structure.
Using 'list' or 'vec' which are not queues.
✗ Incorrect
The queue variable is commonly named 'q' for queue, which is used for level order traversal.
3fill in blank
hardFix the error in the loop condition to process nodes level by level.
DSA C++
while (![1].empty()) {
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'queue' which is the type, not the variable name.
Using 'stack' or 'list' which are not the queue variable.
✗ Incorrect
The queue variable is named 'q', so the loop should check if 'q' is not empty to continue processing.
4fill in blank
hardFill both blanks to correctly calculate the width of the current level.
DSA C++
unsigned long long width = last - [1] + 1; maxWidth = std::max(maxWidth, width[2]);
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using subtraction instead of addition when calculating width.
Using wrong variable names or operators.
✗ Incorrect
Width is calculated as last - first + 1. Then maxWidth is updated with the maximum of current maxWidth and width + 0 (width itself).
5fill in blank
hardFill all three blanks to correctly push child nodes into the queue with updated indices.
DSA C++
if (node->left) q.push({node->left, [1] * 2}); if (node->right) q.push({node->right, [2] * 2 [3] 1});
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using subtraction instead of addition for right child index.
Using wrong variable names.
✗ Incorrect
For left child, index is doubled. For right child, index is doubled and incremented by 1.