📖 Scenario: You are working on a simple program to find the height of a family tree. Each person can have up to two children. The height tells us how many generations are in the family.
🎯 Goal: Build a binary tree representing a family and write code to find its height.
📋 What You'll Learn
Create a binary tree node class called TreeNode with value, left, and right properties
Create a binary tree with exactly 3 levels using TreeNode
Write a function called height that calculates the height of the binary tree
Print the height of the binary tree
💡 Why This Matters
🌍 Real World
Binary trees are used in many real-world applications like family trees, file systems, and decision-making processes.
💼 Career
Understanding tree structures and recursion is important for software development roles, especially in algorithms and data structure interviews.
Progress0 / 4 steps
1
Create TreeNode class and root node
Create a class called TreeNode with a constructor that takes value and sets left and right to null. Then create a root node called root with value 1.
DSA Javascript
Hint
Remember to set left and right to null inside the constructor.
2
Add children to root to form 3 levels
Add two children to root: root.left with value 2 and root.right with value 3. Then add one child to root.left: root.left.left with value 4.
DSA Javascript
Hint
Assign new TreeNode objects to root.left, root.right, and root.left.left.
3
Write function to find height of binary tree
Write a function called height that takes a node and returns 0 if the node is null. Otherwise, return 1 + Math.max(height(node.left), height(node.right)).
DSA Javascript
Hint
Use recursion to find the height of left and right subtrees, then add 1.
4
Print the height of the binary tree
Use console.log to print the height of the tree by calling height(root).
DSA Javascript
Hint
The height of this tree is 3 because it has 3 levels.
Practice
(1/5)
1. What does the height of a binary tree represent?
easy
A. The number of children of the root node
B. The total number of nodes in the tree
C. The number of leaf nodes in the tree
D. The number of edges on the longest path from root to a leaf
Solution
Step 1: Understand the definition of height in a binary tree
The height is the longest path from the root node down to the farthest leaf node, counted by edges.
Step 2: Differentiate height from other tree properties
Number of nodes or leaf nodes are different concepts; height specifically counts edges on the longest path.
Final Answer:
The number of edges on the longest path from root to a leaf -> Option D
Quick Check:
Height = longest root-to-leaf edge count [OK]
Hint: Height counts edges, not nodes, on longest root-to-leaf path [OK]
Common Mistakes:
Confusing height with total nodes count
Counting nodes instead of edges for height
Thinking height equals number of leaf nodes
2. Which of the following is the correct base case to find the height of a binary tree recursively in JavaScript?
easy
A. if (node === null) return -1;
B. if (node === null) return 0;
C. if (node === undefined) return 1;
D. if (node === undefined) return 0;
Solution
Step 1: Understand the base case for empty subtree
When the node is null (no node), height is defined as -1 to count edges properly.
Step 2: Why -1 instead of 0?
Returning -1 for null ensures leaf nodes have height 0 (0 edges below them), matching edge count definition.
Final Answer:
if (node === null) return -1; -> Option A
Quick Check:
Empty node height = -1 for correct edge count [OK]
Hint: Use -1 for null node to count edges correctly [OK]
Common Mistakes:
Returning 0 for null node causes off-by-one height
Checking undefined instead of null for nodes
Confusing node absence with leaf node
3. What is the output of the following code snippet?
class Node {
constructor(val) {
this.val = val;
this.left = null;
this.right = null;
}
}
function height(node) {
if (node === null) return -1;
return 1 + Math.max(height(node.left), height(node.right));
}
const root = new Node(10);
root.left = new Node(5);
root.right = new Node(15);
root.left.left = new Node(3);
root.left.left.left = new Node(1);
console.log(height(root));
medium
A. 5
B. 3
C. 4
D. 2
Solution
Step 1: Trace the tree structure and count edges
The longest path is from root (10) to left-left-left (1): edges are 10->5, 5->3, 3->1, total 3 edges.
Step 2: Calculate height using the function logic
Function returns 1 + max height of children. For leaf node (1), height = 0. So total height = 3 edges means function returns 3.
Final Answer:
3 -> Option B
Quick Check:
Longest path edges = 3 [OK]
Hint: Count edges on longest path, not nodes [OK]
Common Mistakes:
Counting nodes instead of edges for height
Misreading tree structure and missing deepest node
Confusing return value for leaf node height
4. Find the error in this height function and select the correct fix:
function height(node) {
if (node === null) return 0;
return Math.max(height(node.left), height(node.right));
}
medium
A. Change base case to return -1 instead of 0
B. Return sum instead of max of left and right heights
C. Add +1 before Math.max to count current node
D. No error, function is correct
Solution
Step 1: Analyze the base case and recursive return
Base case returns 0 for null, which is acceptable if counting nodes, but function misses adding 1 for current node.
Step 2: Identify missing increment for current node
Function returns max height of children but does not add 1 for current node, so height is underestimated by 1.
Final Answer:
Add +1 before Math.max to count current node -> Option C
Quick Check:
Height = 1 + max(child heights) [OK]
Hint: Add +1 to include current node in height [OK]
Common Mistakes:
Forgetting to add 1 for current node
Changing base case unnecessarily
Returning sum instead of max height
5. Given a binary tree where some nodes have only one child, how does the height calculation change if you want to count the number of nodes on the longest path instead of edges? Choose the correct modification to the recursive height function.
function height(node) {
if (node === null) return ?;
return ? + Math.max(height(node.left), height(node.right));
}
hard
A. Return 0 for null; add 1 before Math.max to count nodes
B. Return -1 for null; add 1 before Math.max to count nodes
C. Return 1 for null; add 0 before Math.max to count nodes
D. Return 0 for null; add 0 before Math.max to count nodes
Solution
Step 1: Understand difference between counting edges and nodes
Counting nodes means height of empty tree is 0, and each node adds 1 to path length.
Step 2: Adjust base case and recursive addition accordingly
Return 0 for null node (no nodes), and add 1 for current node to count nodes on path.
Final Answer:
Return 0 for null; add 1 before Math.max to count nodes -> Option A
Quick Check:
Height counts nodes: base=0, add 1 per node [OK]
Hint: Use 0 base and add 1 to count nodes, not edges [OK]
Common Mistakes:
Using -1 base for node count causes off-by-one
Not adding 1 for current node
Confusing edges count with nodes count
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.