0
0
DSA C++programming~10 mins

Height 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 return the height of the binary tree when the node is null.

DSA C++
int height(Node* root) {
    if (root == [1]) {
        return 0;
    }
    // rest of the code
}
Drag options to blanks, or click blank then click option'
A0
Bnullptr
Croot
DNULL
Attempts:
3 left
💡 Hint
Common Mistakes
Using 0 instead of NULL for pointer comparison.
Using root instead of NULL in the condition.
2fill in blank
medium

Complete the code to calculate the height of the left subtree.

DSA C++
int height(Node* root) {
    if (root == NULL) {
        return 0;
    }
    int leftHeight = height([1]);
    // rest of the code
}
Drag options to blanks, or click blank then click option'
Anullptr
Broot->left
Croot
Droot->right
Attempts:
3 left
💡 Hint
Common Mistakes
Using root->right instead of root->left.
Passing root instead of a child node.
3fill in blank
hard

Fix the error in the return statement to compute the height correctly.

DSA C++
int height(Node* root) {
    if (root == NULL) {
        return 0;
    }
    int leftHeight = height(root->left);
    int rightHeight = height(root->right);
    return 1 + [1](leftHeight, rightHeight);
}
Drag options to blanks, or click blank then click option'
Aabs
Bmin
Cmax
Dsum
Attempts:
3 left
💡 Hint
Common Mistakes
Using min instead of max.
Using sum or abs which are incorrect here.
4fill in blank
hard

Fill both blanks to complete the recursive height calculation with correct function and base case check.

DSA C++
int height(Node* root) {
    if (root == [1]) {
        return [2];
    }
    int leftHeight = height(root->left);
    int rightHeight = height(root->right);
    return 1 + std::max(leftHeight, rightHeight);
}
Drag options to blanks, or click blank then click option'
ANULL
B0
Cnullptr
D1
Attempts:
3 left
💡 Hint
Common Mistakes
Returning 1 instead of 0 for the base case.
Using NULL instead of nullptr in modern C++.
5fill in blank
hard

Fill all three blanks to create a dictionary comprehension-like map of node values to their heights for nodes with height greater than 1.

DSA C++
std::map<int, int> nodeHeights;
for (Node* node : nodes) {
    int h = height(node);
    if (h [1] 1) {
        nodeHeights[[2]] = [3];
    }
}
Drag options to blanks, or click blank then click option'
A>
Bnode->value
Ch
D<=
Attempts:
3 left
💡 Hint
Common Mistakes
Using <= instead of > in the condition.
Using node instead of node->value as the key.
Assigning node->value instead of height as the value.