Bird
Raised Fist0
DSA Typescriptprogramming~20 mins

Height of Binary Tree in DSA Typescript - Practice Problems & Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Binary Tree Height Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
What is the height of this binary tree?

Consider the following binary tree structure and code to calculate its height. What will be the output?

DSA Typescript
class Node {
  val: number;
  left: Node | null;
  right: Node | null;
  constructor(val: number) {
    this.val = val;
    this.left = null;
    this.right = null;
  }
}

function height(root: Node | null): number {
  if (!root) return 0;
  return 1 + Math.max(height(root.left), height(root.right));
}

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));
A4
B3
C5
D2
Attempts:
2 left
💡 Hint

Height counts the number of nodes on the longest path from root to leaf.

❓ Predict Output
intermediate
2:00remaining
Output of height function on a skewed tree

What is the output of the height function for this skewed binary tree?

DSA Typescript
class Node {
  val: number;
  left: Node | null;
  right: Node | null;
  constructor(val: number) {
    this.val = val;
    this.left = null;
    this.right = null;
  }
}

function height(root: Node | null): number {
  if (!root) return 0;
  return 1 + Math.max(height(root.left), height(root.right));
}

const root = new Node(1);
root.right = new Node(2);
root.right.right = new Node(3);
root.right.right.right = new Node(4);

console.log(height(root));
A1
B3
C2
D4
Attempts:
2 left
💡 Hint

Height is the longest path from root to leaf. Here, the tree is skewed to the right.

🧠 Conceptual
advanced
1:30remaining
What does the height of a binary tree represent?

Choose the best description of what the height of a binary tree means.

AThe number of nodes on the longest path from root to a leaf node.
BThe number of nodes in the tree.
CThe number of leaf nodes in the tree.
DThe number of edges on the longest path from root to a leaf node.
Attempts:
2 left
💡 Hint

Height counts nodes, not edges or total nodes.

❓ Predict Output
advanced
1:00remaining
Height of an empty tree

What will be the output of the height function when the tree is empty (root is null)?

DSA Typescript
function height(root: any): number {
  if (!root) return 0;
  return 1 + Math.max(height(root.left), height(root.right));
}

console.log(height(null));
A0
B1
Cnull
Dundefined
Attempts:
2 left
💡 Hint

Think about what the function returns when root is null.

🔧 Debug
expert
2:30remaining
Why does this height function cause a runtime error?

Consider this height function. It causes a runtime error on some inputs. Why?

DSA Typescript
function height(root: any): number {
  if (root === null) return 0;
  return 1 + Math.max(height(root.left), height(root.right));
}

const root = { val: 1, left: null };
console.log(height(root));
ABecause the function does not handle leaf nodes correctly.
BBecause root.left is null, causing infinite recursion.
CBecause root.right is undefined, causing an error when accessed.
DBecause the function is missing a return statement.
Attempts:
2 left
💡 Hint

Check if all child nodes exist before accessing their properties.

Practice

(1/5)
1. What does the height of a binary tree represent?
easy
A. The number of edges on the longest path from root to a leaf
B. The total number of nodes in the tree
C. The number of leaf nodes in the tree
D. The number of nodes at the root level

Solution

  1. 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.
  2. 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.
  3. Final Answer:

    The number of edges on the longest path from root to a leaf -> Option A
  4. 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 TypeScript function signature correctly defines a recursive function to find the height of a binary tree node?
easy
A. function height(node: TreeNode | null): number
B. function height(node: number): number
C. function height(): void
D. function height(node: TreeNode): string

Solution

  1. Step 1: Identify the correct parameter type for a tree node

    The function must accept a node or null (for empty subtree), so type is TreeNode | null.
  2. Step 2: Confirm the return type is number

    The height is a numeric value, so the return type must be number.
  3. Final Answer:

    function height(node: TreeNode | null): number -> Option A
  4. Quick Check:

    Parameter: TreeNode|null, Return: number [OK]
Hint: Height function needs node or null input and returns number [OK]
Common Mistakes:
  • Using wrong parameter type like number instead of node
  • Returning void or string instead of number
  • Missing null to handle empty subtree
3. What is the height of the following binary tree?
    1
   / \
  2   3
 / 
4  

Consider height as number of edges on longest path from root to leaf.
medium
A. 3
B. 4
C. 1
D. 2

Solution

  1. Step 1: Identify all root-to-leaf paths and count edges

    Paths: 1->2->4 has 2 edges, 1->3 has 1 edge.
  2. Step 2: Find the longest path edge count

    Longest path is 1->2->4 with 2 edges.
  3. Final Answer:

    2 -> Option D
  4. Quick Check:

    Longest path edges = 2 [OK]
Hint: Count edges, not nodes, on longest root-to-leaf path [OK]
Common Mistakes:
  • Counting nodes instead of edges
  • Choosing shorter path length
  • Confusing height with tree depth
4. Identify the error in this TypeScript function to find binary tree height:
function height(node: TreeNode | null): number {
  if (!node) return 0;
  return Math.max(height(node.left), height(node.right));
}
medium
A. Returns 0 for null node instead of -1
B. Uses Math.min instead of Math.max
C. Missing +1 to count current node edge
D. Function should return void

Solution

  1. Step 1: Analyze base case return value

    Returning 0 for null means empty subtree height is 0, which is acceptable if counting edges from nodes.
  2. Step 2: Check recursive return statement

    Function returns max height of children but misses adding 1 for current edge from node to child.
  3. Final Answer:

    Missing +1 to count current node edge -> Option C
  4. Quick Check:

    Height = max child height + 1 edge [OK]
Hint: Add +1 after max to include current node edge [OK]
Common Mistakes:
  • Forgetting to add 1 for current node edge
  • Returning wrong base case value
  • Using min instead of max for height
5. Given a binary tree where each node has a value, write a function to find the height of the tree but only counting nodes with even values. What is the height of this tree?
    2
   / \
  3   4
 /   / \
6   5   8
hard
A. 0
B. 2
C. 1
D. 3

Solution

  1. Step 1: Identify nodes with even values and their connections

    Even nodes: 2 (root), 4 (right child), 6 (left-left child), 8 (right-right child). Paths counting only even nodes:
    • 2 -> 4 -> 8 (edges = 2)
    • 2 -> 3 (3 is odd, so path ends at 2)
    • 2 -> 3 -> 6 (6 is even but 3 is odd, so path breaks at 3)
  2. Step 2: Calculate height counting only edges between even nodes

    Longest path with even nodes is 2 -> 4 -> 8 with 2 edges.
  3. Final Answer:

    2 -> Option B
  4. Quick Check:

    Longest even-valued node path edges = 2 [OK]
Hint: Count edges only between even-valued nodes [OK]
Common Mistakes:
  • Counting odd nodes in path
  • Counting nodes instead of edges
  • Ignoring breaks caused by odd nodes
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard