0
0
DSA C++programming~10 mins

Diameter of Binary Tree in DSA C++ - Interactive Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete 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'
AleftHeight - rightHeight
Bmin(leftHeight, rightHeight)
CleftHeight + rightHeight
Dmax(leftHeight, rightHeight)
Attempts:
3 left
💡 Hint
Common Mistakes
Using min instead of max for height calculation.
Adding left and right heights instead of taking max.
2fill in blank
medium

Complete 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'
A0
B-1
C1
D2
Attempts:
3 left
💡 Hint
Common Mistakes
Adding 0 instead of 1, missing the current node.
Adding 2 or -1 causing wrong diameter count.
3fill in blank
hard

Fix 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'
Adiameter
Bheight(root)
C0
Droot->val
Attempts:
3 left
💡 Hint
Common Mistakes
Returning height instead of diameter.
Returning 0 or root value instead of diameter.
4fill in blank
hard

Fill 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'
Aleft
Bright
Cparent
Dchild
Attempts:
3 left
💡 Hint
Common Mistakes
Using parent or child which are not valid pointers here.
Swapping left and right causing confusion.
5fill in blank
hard

Fill 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'
Anode
Croot
Dthis
Attempts:
3 left
💡 Hint
Common Mistakes
Using inconsistent variable names causing errors.
Calling height with 'this' or wrong variable.