0
0
DSA C++programming~10 mins

Tree Terminology Root Leaf Height Depth Level 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 define the root node of a tree.

DSA C++
struct Node {
    int data;
    Node* left;
    Node* right;
};

Node* [1] = nullptr;  // Root node pointer
Drag options to blanks, or click blank then click option'
Aroot
Bheight
Cleaf
Dlevel
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'leaf' instead of 'root' for the top node pointer.
Confusing 'height' or 'level' as variable names for the root.
2fill in blank
medium

Complete the code to check if a node is a leaf (no children).

DSA C++
bool isLeaf(Node* node) {
    return node->left == nullptr && node->[1] == nullptr;
}
Drag options to blanks, or click blank then click option'
Aright
Bparent
Croot
Dlevel
Attempts:
3 left
💡 Hint
Common Mistakes
Checking only one child pointer.
Using 'parent' or 'root' instead of 'right' pointer.
3fill in blank
hard

Fix the error in the function that calculates the height of a tree node.

DSA C++
int height(Node* node) {
    if (node == nullptr) return 0;
    int leftHeight = height(node->left);
    int rightHeight = height(node->[1]);
    return 1 + (leftHeight > rightHeight ? leftHeight : rightHeight);
}
Drag options to blanks, or click blank then click option'
Alevel
Bparent
Croot
Dright
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'parent' or 'root' instead of 'right' pointer.
Returning incorrect height calculation.
4fill in blank
hard

Fill both blanks to create a function that returns the level (depth) of a node in the tree.

DSA C++
int getLevel(Node* root, Node* node, int level) {
    if (root == nullptr) return 0;
    if (root == node) return [1];
    int downLevel = getLevel(root->left, node, level [2] 1);
    if (downLevel != 0) return downLevel;
    return getLevel(root->right, node, level [2] 1);
}
Drag options to blanks, or click blank then click option'
Alevel
Blevel + 1
Clevel - 1
Dlevel * 1
Attempts:
3 left
💡 Hint
Common Mistakes
Returning 0 instead of current level when node is found.
Not increasing level when going deeper.
5fill in blank
hard

Fill all three blanks to create a function that prints nodes at a given level.

DSA C++
void printLevelNodes(Node* root, int level) {
    if (root == nullptr) return;
    if (level == [1]) {
        std::cout << root->data << " ";
    } else if (level > [2]) {
        printLevelNodes(root->left, level [3] 1);
        printLevelNodes(root->right, level [3] 1);
    }
}
Drag options to blanks, or click blank then click option'
A1
B0
C-
D+
Attempts:
3 left
💡 Hint
Common Mistakes
Checking if level == 1 instead of 0.
Increasing level instead of decreasing when going down.