0
0
DSA C++programming~20 mins

Height of Binary Tree in DSA C++ - Practice Problems & Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Height Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of height calculation on a simple binary tree
What is the output of the following C++ code that calculates the height of a binary tree?
DSA C++
#include <iostream>

struct Node {
    int data;
    Node* left;
    Node* right;
    Node(int val) : data(val), left(nullptr), right(nullptr) {}
};

int height(Node* root) {
    if (!root) return 0;
    int leftHeight = height(root->left);
    int rightHeight = height(root->right);
    return 1 + (leftHeight > rightHeight ? leftHeight : rightHeight);
}

int main() {
    Node* root = new Node(1);
    root->left = new Node(2);
    root->right = new Node(3);
    root->left->left = new Node(4);
    root->left->right = new Node(5);
    int h = height(root);
    std::cout << h << std::endl;
    return 0;
}
A3
B2
C4
D5
Attempts:
2 left
💡 Hint
Count the longest path from root to leaf node.
🧠 Conceptual
intermediate
1:00remaining
Understanding height of an empty tree
What is the height of an empty binary tree (where the root is nullptr)?
A-1
B1
C0
DUndefined
Attempts:
2 left
💡 Hint
Think about the base case in the height function.
Predict Output
advanced
2:00remaining
Height of an unbalanced binary tree
What is the output of the following code that calculates the height of an unbalanced binary tree?
DSA C++
#include <iostream>

struct Node {
    int data;
    Node* left;
    Node* right;
    Node(int val) : data(val), left(nullptr), right(nullptr) {}
};

int height(Node* root) {
    if (!root) return 0;
    int leftHeight = height(root->left);
    int rightHeight = height(root->right);
    return 1 + (leftHeight > rightHeight ? leftHeight : rightHeight);
}

int main() {
    Node* root = new Node(10);
    root->left = new Node(20);
    root->left->left = new Node(40);
    root->left->left->left = new Node(80);
    root->right = new Node(30);
    int h = height(root);
    std::cout << h << std::endl;
    return 0;
}
A3
B4
C5
D2
Attempts:
2 left
💡 Hint
Check the longest path from root to leaf on the left side.
🔧 Debug
advanced
2:00remaining
Identify the error in height calculation code
What error will the following code produce when calculating the height of a binary tree?
DSA C++
#include <iostream>

struct Node {
    int data;
    Node* left;
    Node* right;
    Node(int val) : data(val), left(nullptr), right(nullptr) {}
};

int height(Node* root) {
    if (root == nullptr) return 0;
    int leftHeight = height(root->left);
    int rightHeight = height(root->right);
    return 1 + (leftHeight < rightHeight ? leftHeight : rightHeight);
}

int main() {
    Node* root = new Node(1);
    root->left = new Node(2);
    root->right = new Node(3);
    std::cout << height(root) << std::endl;
    return 0;
}
ARuntime error due to null pointer dereference
BSyntax error due to missing semicolon
COutputs zero always
DThe code outputs the minimum height instead of maximum height
Attempts:
2 left
💡 Hint
Check the comparison operator used to select height.
🚀 Application
expert
3:00remaining
Height of a binary tree after inserting nodes in a specific order
Given an initially empty binary tree, nodes are inserted in this order: 15, 10, 20, 8, 12, 17, 25, 6. What is the height of the tree after all insertions?
DSA C++
#include <iostream>

struct Node {
    int data;
    Node* left;
    Node* right;
    Node(int val) : data(val), left(nullptr), right(nullptr) {}
};

Node* insert(Node* root, int val) {
    if (!root) return new Node(val);
    if (val < root->data) root->left = insert(root->left, val);
    else root->right = insert(root->right, val);
    return root;
}

int height(Node* root) {
    if (!root) return 0;
    int leftHeight = height(root->left);
    int rightHeight = height(root->right);
    return 1 + (leftHeight > rightHeight ? leftHeight : rightHeight);
}

int main() {
    Node* root = nullptr;
    int values[] = {15, 10, 20, 8, 12, 17, 25, 6};
    for (int v : values) {
        root = insert(root, v);
    }
    std::cout << height(root) << std::endl;
    return 0;
}
A4
B3
C5
D6
Attempts:
2 left
💡 Hint
Draw the tree after insertions and count the longest path.