0
0
DSA Typescriptprogramming~20 mins

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

Choose your learning style9 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.