0
0
DSA Javascriptprogramming~20 mins

Diameter of Binary Tree in DSA Javascript - 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 a simple binary tree
What is the output of the following code that calculates the diameter of a binary tree?
DSA Javascript
class TreeNode {
  constructor(val, left = null, right = null) {
    this.val = val;
    this.left = left;
    this.right = right;
  }
}

function diameterOfBinaryTree(root) {
  let diameter = 0;
  function depth(node) {
    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 root = new TreeNode(1, new TreeNode(2, new TreeNode(4), new TreeNode(5)), new TreeNode(3));
console.log(diameterOfBinaryTree(root));
A2
B5
C4
D3
Attempts:
2 left
💡 Hint
The diameter is the longest path between any two nodes, counting edges.
Predict Output
intermediate
2:00remaining
Diameter of a skewed binary tree
What is the output of the diameterOfBinaryTree function for this skewed tree?
DSA Javascript
const root = new TreeNode(1, new TreeNode(2, new TreeNode(3, new TreeNode(4))));
console.log(diameterOfBinaryTree(root));
A3
B4
C0
D1
Attempts:
2 left
💡 Hint
In a skewed tree, the diameter is the number of edges in the longest chain.
🔧 Debug
advanced
2:00remaining
Identify the error in this diameter calculation code
What error will this code produce when calculating the diameter of a binary tree?
DSA Javascript
function diameterOfBinaryTree(root) {
  let diameter = 0;
  function depth(node) {
    if (node === null) return 0;
    const left = depth(node.left);
    const right = depth(node.right);
    diameter = Math.max(diameter, left + right + 1);
    return Math.max(left, right) + 1;
  }
  depth(root);
  return diameter;
}
ATypeError because node.left might be undefined
BSyntaxError due to missing return statement
CThe diameter is off by 1 (returns number of nodes, not edges)
DNo error, returns correct diameter
Attempts:
2 left
💡 Hint
Diameter counts edges, but code adds 1 extra edge.
🧠 Conceptual
advanced
2:00remaining
Understanding diameter calculation logic
Why does the diameter calculation use the sum of left and right subtree depths at each node?
ABecause the longest path passes through the current node connecting left and right subtrees
BBecause the diameter is the maximum depth of either subtree alone
CBecause the diameter is the sum of all nodes in the tree
DBecause the diameter is the height of the tree plus one
Attempts:
2 left
💡 Hint
Think about the longest path crossing the root of a subtree.
Predict Output
expert
2:00remaining
Diameter of a complex binary tree with multiple branches
What is the output of the diameterOfBinaryTree function for this tree?
DSA Javascript
const root = new TreeNode(1,
  new TreeNode(2,
    new TreeNode(4,
      new TreeNode(7),
      null
    ),
    new TreeNode(5)
  ),
  new TreeNode(3,
    null,
    new TreeNode(6,
      new TreeNode(8),
      new TreeNode(9)
    )
  )
);
console.log(diameterOfBinaryTree(root));
A4
B6
C7
D5
Attempts:
2 left
💡 Hint
Trace the longest path between two leaves in different branches.