Challenge - 5 Problems
Height Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
What is the height of this binary tree?
Consider the following binary tree structure and code to calculate its height. What is the output printed by the code?
DSA Javascript
class Node { constructor(value) { this.value = value; this.left = null; this.right = null; } } function height(root) { if (!root) return 0; const leftHeight = height(root.left); const rightHeight = height(root.right); return Math.max(leftHeight, rightHeight) + 1; } const 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); console.log(height(root));
Attempts:
2 left
💡 Hint
Think about the longest path from the root to a 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, which has 3 nodes, so height is 3.
❓ Predict Output
intermediate2:00remaining
Output of height function on a skewed tree
What will be the output of the height function when called on this skewed binary tree?
DSA Javascript
class Node { constructor(value) { this.value = value; this.left = null; this.right = null; } } function height(root) { if (!root) return 0; return Math.max(height(root.left), height(root.right)) + 1; } const root = new Node(10); root.right = new Node(20); root.right.right = new Node(30); root.right.right.right = new Node(40); console.log(height(root));
Attempts:
2 left
💡 Hint
Count the nodes along the right side.
✗ Incorrect
The tree is skewed to the right with 4 nodes in a line, so height is 4.
🔧 Debug
advanced2:00remaining
Identify the error in height calculation
The following code is intended to calculate the height of a binary tree. What error will it produce when run?
DSA Javascript
function height(root) {
if (root == null) {
return -1;
}
const leftHeight = height(root.left);
const rightHeight = height(root.right);
return Math.max(leftHeight, rightHeight);
}
const root = { value: 1, left: null, right: null };
console.log(height(root));Attempts:
2 left
💡 Hint
Check the base case and the return statement.
✗ Incorrect
The base case returns -1 for null nodes, but the function does not add 1 after max, so height of a single node returns -1 instead of 0.
🧠 Conceptual
advanced1:00remaining
Height of an empty tree
What is the height of an empty binary tree (null root)?
Attempts:
2 left
💡 Hint
Think about how height is defined for no nodes.
✗ Incorrect
An empty tree has height 0 because there are no nodes.
🚀 Application
expert3:00remaining
Calculate height after inserting nodes
Given an initially empty binary tree, nodes are inserted in this order: 5, 3, 8, 1, 4, 7, 9. What is the height of the tree after all insertions?
Attempts:
2 left
💡 Hint
Visualize the tree structure after inserting nodes in given order.
✗ Incorrect
The tree is balanced with height 3 after inserting nodes in given order.