0
0
DSA Javascriptprogramming~10 mins

Diameter of Binary Tree in DSA Javascript - Interactive Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to calculate the height of a binary tree node.

DSA Javascript
function height(node) {
  if (node === null) return 0;
  return 1 + Math.max(height(node.left), height(node.[1]));
}
Drag options to blanks, or click blank then click option'
Aparent
Broot
Cright
Dchild
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'parent' instead of 'right' causes undefined errors.
Using 'child' or 'root' are not valid properties of a node.
2fill in blank
medium

Complete the code to update the diameter during traversal.

DSA Javascript
function diameterOfBinaryTree(root) {
  let diameter = 0;
  function dfs(node) {
    if (node === null) return 0;
    let leftHeight = dfs(node.left);
    let rightHeight = dfs(node.right);
    diameter = Math.max(diameter, leftHeight + rightHeight);
    return 1 + Math.max(leftHeight, [1]);
  }
  dfs(root);
  return diameter;
}
Drag options to blanks, or click blank then click option'
Anode
Bdiameter
Croot
DrightHeight
Attempts:
3 left
💡 Hint
Common Mistakes
Returning diameter instead of rightHeight causes wrong height calculation.
Returning node or root is incorrect as they are objects, not heights.
3fill in blank
hard

Fix the error in the recursive function call to avoid infinite recursion.

DSA Javascript
function dfs(node) {
  if (node === null) return 0;
  let leftHeight = dfs(node.left);
  let rightHeight = dfs(node.[1]);
  return 1 + Math.max(leftHeight, rightHeight);
}
Drag options to blanks, or click blank then click option'
Aright
Bleft
Cparent
Dnode
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'left' twice causes missing right subtree traversal.
Using 'parent' causes infinite recursion or errors.
4fill in blank
hard

Fill both blanks to correctly update the diameter and return the height.

DSA Javascript
function dfs(node) {
  if (node === null) return 0;
  let leftHeight = dfs(node.left);
  let rightHeight = dfs(node.right);
  diameter = Math.max(diameter, leftHeight [1] rightHeight);
  return 1 [2] Math.max(leftHeight, rightHeight);
}
Drag options to blanks, or click blank then click option'
A+
B-
C*
D/
Attempts:
3 left
💡 Hint
Common Mistakes
Using '-' or '*' causes wrong diameter or height calculations.
Using '/' causes runtime errors or wrong results.
5fill in blank
hard

Fill all three blanks to complete the diameter calculation using a helper function.

DSA Javascript
function diameterOfBinaryTree(root) {
  let diameter = 0;
  function dfs(node) {
    if (node === null) return 0;
    let leftHeight = dfs(node.left);
    let rightHeight = dfs(node.right);
    diameter = Math.max(diameter, leftHeight [1] rightHeight);
    return 1 + Math.max(leftHeight, [2]);
  }
  dfs([3]);
  return diameter;
}
Drag options to blanks, or click blank then click option'
A+
BrightHeight
Croot
DleftHeight
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong operators like '-' or '*' for diameter update.
Returning wrong variable instead of rightHeight.
Calling dfs on leftHeight or rightHeight instead of root.