Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to calculate the height of a binary tree node.
DSA C++
int height(TreeNode* node) {
if (node == nullptr) return 0;
int leftHeight = height(node->left);
int rightHeight = height(node->right);
return 1 + [1];
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using min instead of max for height calculation.
Adding left and right heights instead of taking max.
✗ Incorrect
The height of a node is 1 plus the maximum height of its left and right subtrees.
2fill in blank
mediumComplete the code to update the diameter while computing height.
DSA C++
int diameter = 0; int height(TreeNode* node) { if (node == nullptr) return 0; int leftHeight = height(node->left); int rightHeight = height(node->right); diameter = std::max(diameter, leftHeight + rightHeight + [1]); return 1 + std::max(leftHeight, rightHeight); }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Adding 0 instead of 1, missing the current node.
Adding 2 or -1 causing wrong diameter count.
✗ Incorrect
We add 1 to count the current node between left and right paths for diameter.
3fill in blank
hardFix the error in the diameter calculation function to return the correct diameter.
DSA C++
int diameterOfBinaryTree(TreeNode* root) {
diameter = 0;
height(root);
return [1];
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Returning height instead of diameter.
Returning 0 or root value instead of diameter.
✗ Incorrect
The diameter variable stores the maximum diameter found, so return it.
4fill in blank
hardFill both blanks to complete the recursive height function that updates diameter.
DSA C++
int height(TreeNode* node) {
if (node == nullptr) return 0;
int leftHeight = height(node->[1]);
int rightHeight = height(node->[2]);
diameter = std::max(diameter, leftHeight + rightHeight + 1);
return 1 + std::max(leftHeight, rightHeight);
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using parent or child which are not valid pointers here.
Swapping left and right causing confusion.
✗ Incorrect
We recursively call height on the left and right child nodes.
5fill in blank
hardFill all three blanks to complete the diameter calculation using a helper function.
DSA C++
class Solution { public: int diameter = 0; int height(TreeNode* [1]) { if ([2] == nullptr) return 0; int leftHeight = height([2]->left); int rightHeight = height([2]->right); diameter = std::max(diameter, leftHeight + rightHeight + 1); return 1 + std::max(leftHeight, rightHeight); } int diameterOfBinaryTree(TreeNode* root) { height([3]); return diameter - 1; } };
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using inconsistent variable names causing errors.
Calling height with 'this' or wrong variable.
✗ Incorrect
The helper function uses 'node' as parameter and variable; diameterOfBinaryTree calls height with root.