Challenge - 5 Problems
Height Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2: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; }
Attempts:
2 left
💡 Hint
Count the longest path from root to leaf node.
✗ Incorrect
The height is the number of nodes on the longest path from the root to a leaf. Here, the longest path is 1 -> 2 -> 4 or 1 -> 2 -> 5, which has 3 nodes.
🧠 Conceptual
intermediate1:00remaining
Understanding height of an empty tree
What is the height of an empty binary tree (where the root is nullptr)?
Attempts:
2 left
💡 Hint
Think about the base case in the height function.
✗ Incorrect
By definition, the height of an empty tree is 0 because there are no nodes.
❓ Predict Output
advanced2: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; }
Attempts:
2 left
💡 Hint
Check the longest path from root to leaf on the left side.
✗ Incorrect
The longest path is 10 -> 20 -> 40 -> 80, which has 4 nodes, so height is 4.
🔧 Debug
advanced2: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; }
Attempts:
2 left
💡 Hint
Check the comparison operator used to select height.
✗ Incorrect
The code uses < instead of > to select between leftHeight and rightHeight, so it returns the minimum height path plus one, not the maximum.
🚀 Application
expert3: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; }
Attempts:
2 left
💡 Hint
Draw the tree after insertions and count the longest path.
✗ Incorrect
After inserting nodes, the longest path is 15 -> 10 -> 8 -> 6, which has 4 nodes, so height is 4.