Bird
Raised Fist0
DSA Typescriptprogramming~20 mins

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

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Diameter Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of Diameter Calculation on Simple Tree
What is the output of the following TypeScript code that calculates the diameter of a binary tree?
DSA Typescript
class TreeNode {
  val: number;
  left: TreeNode | null;
  right: TreeNode | null;
  constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
    this.val = val === undefined ? 0 : val;
    this.left = left === undefined ? null : left;
    this.right = right === undefined ? null : right;
  }
}

function diameterOfBinaryTree(root: TreeNode | null): number {
  let diameter = 0;
  function depth(node: TreeNode | null): number {
    if (!node) return 0;
    const left = depth(node.left);
    const right = depth(node.right);
    diameter = Math.max(diameter, left + right);
    return Math.max(left, right) + 1;
  }
  depth(root);
  return diameter;
}

const tree = new TreeNode(1, new TreeNode(2, new TreeNode(4), new TreeNode(5)), new TreeNode(3));
console.log(diameterOfBinaryTree(tree));
A5
B2
C4
D3
Attempts:
2 left
💡 Hint
Think about the longest path between any two nodes in the tree.
🧠 Conceptual
intermediate
1:30remaining
Understanding Diameter Definition
Which of the following best describes the diameter of a binary tree?
AThe number of edges in the longest path between any two nodes.
BThe height of the tree from root to the deepest leaf.
CThe number of nodes in the longest path between any two nodes.
DThe total number of nodes in the tree.
Attempts:
2 left
💡 Hint
Diameter counts edges, not nodes.
🔧 Debug
advanced
2:00remaining
Identify the Bug in Diameter Calculation
What error will the following TypeScript code produce when calculating the diameter of a binary tree?
DSA Typescript
class TreeNode {
  val: number;
  left: TreeNode | null;
  right: TreeNode | null;
  constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
    this.val = val === undefined ? 0 : val;
    this.left = left === undefined ? null : left;
    this.right = right === undefined ? null : right;
  }
}

function diameterOfBinaryTree(root: TreeNode | null): number {
  let diameter = 0;
  function depth(node: TreeNode | null): number {
    if (!node) return 0;
    const left = depth(node.left);
    const right = depth(node.right);
    diameter = Math.max(diameter, left + right);
    return Math.max(left, right);
  }
  depth(root);
  return diameter;
}

const tree = new TreeNode(1, new TreeNode(2), new TreeNode(3));
console.log(diameterOfBinaryTree(tree));
AThe code runs correctly and outputs 2.
BThe code outputs 0 because the depth function does not add 1 to the return value.
CThe code throws a TypeError due to null reference.
DThe code outputs 0 because diameter is never updated.
Attempts:
2 left
💡 Hint
Check the return statement of the depth function.
🚀 Application
advanced
2:00remaining
Diameter of Unbalanced Binary Tree
Given the following unbalanced binary tree, what is the diameter?
DSA Typescript
class TreeNode {
  val: number;
  left: TreeNode | null;
  right: TreeNode | null;
  constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
    this.val = val === undefined ? 0 : val;
    this.left = left === undefined ? null : left;
    this.right = right === undefined ? null : right;
  }
}

const tree = new TreeNode(1, new TreeNode(2, new TreeNode(3, new TreeNode(4), null), null), new TreeNode(5));
A5
B3
C4
D6
Attempts:
2 left
💡 Hint
Find the longest path between two nodes in the tree.
❓ Predict Output
expert
2:30remaining
Output of Diameter Calculation with Multiple Paths
What is the output of the following TypeScript code that calculates the diameter of a binary tree with multiple longest paths?
DSA Typescript
class TreeNode {
  val: number;
  left: TreeNode | null;
  right: TreeNode | null;
  constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
    this.val = val === undefined ? 0 : val;
    this.left = left === undefined ? null : left;
    this.right = right === undefined ? null : right;
  }
}

function diameterOfBinaryTree(root: TreeNode | null): number {
  let diameter = 0;
  function depth(node: TreeNode | null): number {
    if (!node) return 0;
    const left = depth(node.left);
    const right = depth(node.right);
    diameter = Math.max(diameter, left + right);
    return Math.max(left, right) + 1;
  }
  depth(root);
  return diameter;
}

const tree = new TreeNode(1,
  new TreeNode(2, new TreeNode(4), new TreeNode(5)),
  new TreeNode(3, null, new TreeNode(6, new TreeNode(7), null))
);
console.log(diameterOfBinaryTree(tree));
A5
B7
C6
D4
Attempts:
2 left
💡 Hint
Consider all longest paths and count edges.

Practice

(1/5)
1. What does the diameter of a binary tree represent?
easy
A. The height of the tree
B. The number of nodes in the tree
C. The number of leaf nodes in the tree
D. The length of the longest path between any two nodes in the tree

Solution

  1. Step 1: Understand the diameter definition

    The diameter is the longest path between any two nodes, not just from root to leaf.
  2. Step 2: Differentiate from other tree properties

    Height is longest root-to-leaf path, number of nodes counts all nodes, leaves count only leaf nodes.
  3. Final Answer:

    The length of the longest path between any two nodes in the tree -> Option D
  4. Quick Check:

    Diameter = longest path between nodes [OK]
Hint: Diameter is longest path between any two nodes, not just height [OK]
Common Mistakes:
  • Confusing diameter with tree height
  • Thinking diameter counts nodes instead of edges
  • Assuming diameter is from root only
2. Which TypeScript function signature correctly represents a function to compute the diameter of a binary tree node?
easy
A. function diameter(root: TreeNode | null): number
B. function diameter(root: number): TreeNode
C. function diameter(root: TreeNode): void
D. function diameter(): number

Solution

  1. Step 1: Identify input and output types

    The function takes a TreeNode or null and returns a number representing diameter length.
  2. Step 2: Match correct signature

    function diameter(root: TreeNode | null): number matches input type TreeNode|null and returns number, others mismatch input/output.
  3. Final Answer:

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

    Input: TreeNode|null, Output: number [OK]
Hint: Diameter function inputs root node, returns number length [OK]
Common Mistakes:
  • Using wrong input type like number instead of TreeNode
  • Returning void instead of number
  • Missing null option for empty tree
3. What is the diameter of the following binary tree?
    1
   / \
  2   3
 / 
4  
medium
A. 2
B. 3
C. 4
D. 1

Solution

  1. Step 1: Identify longest path in tree

    Longest path is from node 4 to node 3 passing through 2 and 1, length 3 edges.
  2. Step 2: Count edges in longest path

    Path 4 -> 2 -> 1 -> 3 has 3 edges, so diameter is 3.
  3. Final Answer:

    3 -> Option B
  4. Quick Check:

    Longest path edges = 3 [OK]
Hint: Count edges on longest path between any two nodes [OK]
Common Mistakes:
  • Counting nodes instead of edges
  • Choosing height instead of diameter
  • Ignoring path through root
4. Given this TypeScript code snippet to compute diameter, what is the error?
function diameter(root: TreeNode | null): number {
  let maxDiameter = 0;
  function depth(node: TreeNode | null): number {
    if (!node) return 0;
    const left = depth(node.left);
    const right = depth(node.right);
    maxDiameter = Math.max(maxDiameter, left + right + 1);
    return Math.max(left, right) + 1;
  }
  depth(root);
  return maxDiameter;
}
medium
A. Diameter counts nodes, should count edges (subtract 1)
B. Missing base case for null node
C. Returns depth instead of diameter
D. maxDiameter is not updated inside depth function

Solution

  1. Step 1: Analyze diameter calculation

    maxDiameter updates with left + right + 1, counting nodes on path.
  2. Step 2: Correct diameter definition

    Diameter is number of edges, so subtract 1 from node count to get edges.
  3. Final Answer:

    Diameter counts nodes, should count edges (subtract 1) -> Option A
  4. Quick Check:

    Diameter edges = nodes - 1 [OK]
Hint: Diameter = nodes on path minus 1 for edges [OK]
Common Mistakes:
  • Confusing nodes count with edges count
  • Forgetting to subtract 1 for edges
  • Incorrect base case handling
5. How would you modify the diameter function to also return the actual longest path nodes in TypeScript?
hard
A. Use BFS to find diameter and path separately after diameter calculation
B. Return only diameter number, path cannot be found during recursion
C. Track and store nodes on longest path during recursion, return path array with diameter
D. Store only leaf nodes and reconstruct path after diameter is found

Solution

  1. Step 1: Understand diameter path tracking

    During recursion, keep track of nodes on left and right depths to build longest path.
  2. Step 2: Modify function to store and return path

    Update maxDiameter and store nodes forming longest path, return both diameter and path array.
  3. Final Answer:

    Track and store nodes on longest path during recursion, return path array with diameter -> Option C
  4. Quick Check:

    Store nodes during recursion to get path [OK]
Hint: Store nodes on longest path during recursion to return path [OK]
Common Mistakes:
  • Assuming path cannot be found during recursion
  • Trying BFS after diameter calculation only
  • Only storing leaf nodes misses full path
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