Challenge - 5 Problems
Binary Tree Height Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateWhat 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 program?
DSA C#
class Node { public int Value; public Node Left, Right; public Node(int val) { Value = val; Left = Right = null; } } class BinaryTree { public Node Root; public int Height(Node node) { if (node == null) return 0; int leftHeight = Height(node.Left); int rightHeight = Height(node.Right); return 1 + (leftHeight > rightHeight ? leftHeight : rightHeight); } } var tree = new BinaryTree(); tree.Root = new Node(1); tree.Root.Left = new Node(2); tree.Root.Right = new Node(3); tree.Root.Left.Left = new Node(4); tree.Root.Left.Right = new Node(5); Console.WriteLine(tree.Height(tree.Root));
Attempts:
2 left
💡 Hint
Count the number of nodes on the longest path from root to leaf.
✗ Incorrect
The height is the number of nodes on the longest path from root to leaf. Here, the longest path is 1 -> 2 -> 4, which has 3 nodes, so height is 3.
❓ Predict Output
intermediateWhat does this code print for an empty tree?
Given the following code, what is the output when the tree is empty (root is null)?
DSA C#
class Node { public int Value; public Node Left, Right; public Node(int val) { Value = val; Left = Right = null; } } class BinaryTree { public Node Root = null; public int Height(Node node) { if (node == null) return 0; int leftHeight = Height(node.Left); int rightHeight = Height(node.Right); return 1 + (leftHeight > rightHeight ? leftHeight : rightHeight); } } var tree = new BinaryTree(); Console.WriteLine(tree.Height(tree.Root));
Attempts:
2 left
💡 Hint
What is the height of a tree with no nodes?
✗ Incorrect
If the root is null, the height function returns 0 immediately, meaning the tree has height zero.
🔧 Debug
advancedWhy does this code cause a stack overflow?
Examine the code below. It is intended to compute the height of a binary tree but causes a stack overflow error. What is the reason?
DSA C#
class Node { public int Value; public Node Left, Right; public Node(int val) { Value = val; Left = Right = null; } } class BinaryTree { public Node Root; public int Height(Node node) { if (node == null) return 0; int leftHeight = Height(node); int rightHeight = Height(node.Right); return 1 + (leftHeight > rightHeight ? leftHeight : rightHeight); } }
Attempts:
2 left
💡 Hint
Check the recursive calls carefully to see if they progress down the tree.
✗ Incorrect
The recursive call Height(node) calls itself with the same node repeatedly, never moving to children, causing infinite recursion and stack overflow.
🧠 Conceptual
advancedWhat is the time complexity of computing the height of a binary tree?
Given a binary tree with n nodes, what is the time complexity of the recursive height calculation method?
Attempts:
2 left
💡 Hint
Consider how many nodes the function visits.
✗ Incorrect
The height function visits every node once to compute heights of subtrees, so it runs in linear time O(n).
🚀 Application
expertWhat is the height of this unbalanced binary tree?
Given the following tree structure, what is the height computed by the Height method?
DSA C#
class Node { public int Value; public Node Left, Right; public Node(int val) { Value = val; Left = Right = null; } } class BinaryTree { public Node Root; public int Height(Node node) { if (node == null) return 0; int leftHeight = Height(node.Left); int rightHeight = Height(node.Right); return 1 + (leftHeight > rightHeight ? leftHeight : rightHeight); } } var tree = new BinaryTree(); tree.Root = new Node(10); tree.Root.Right = new Node(20); tree.Root.Right.Right = new Node(30); tree.Root.Right.Right.Right = new Node(40); Console.WriteLine(tree.Height(tree.Root));
Attempts:
2 left
💡 Hint
Count nodes along the longest path from root to leaf.
✗ Incorrect
The longest path is 10 -> 20 -> 30 -> 40, which has 4 nodes, so height is 4.
