0
0
DSA Typescriptprogramming~20 mins

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

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