0
0
DSA Typescriptprogramming~10 mins

Diameter of Binary Tree in DSA Typescript - Interactive Practice

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

Complete the code to define the TreeNode class with left and right children.

DSA Typescript
class TreeNode {
  val: number;
  left: TreeNode | null;
  right: TreeNode | null;
  constructor(val: number) {
    this.val = val;
    this.left = [1];
    this.right = null;
  }
}
Drag options to blanks, or click blank then click option'
A0
Bundefined
Cnull
Dthis
Attempts:
3 left
💡 Hint
Common Mistakes
Using undefined instead of null for children.
Assigning 0 or this which are incorrect types.
2fill in blank
medium

Complete the code to calculate the maximum of two numbers.

DSA Typescript
function max(a: number, b: number): number {
  return a [1];
}
Drag options to blanks, or click blank then click option'
A+
B> b ? a : b
CMath.max(a, b)
D>
Attempts:
3 left
💡 Hint
Common Mistakes
Using '+' which adds numbers instead of comparing.
Using '>' alone which returns a boolean, not a number.
Using Math.max(a, b) inside return without proper syntax.
3fill in blank
hard

Fix the error in the recursive function to compute the height of a binary tree node.

DSA Typescript
function height(node: TreeNode | null): number {
  if (node === null) return 0;
  return 1 + Math.max(height(node.left), [1]);
}
Drag options to blanks, or click blank then click option'
Anode.left
Bheight(node.left)
Cnode.right
Dheight(node.right)
Attempts:
3 left
💡 Hint
Common Mistakes
Calling height(node.left) twice instead of right child.
Using node.right directly instead of recursive call.
4fill in blank
hard

Fill both blanks to update the diameter while computing height.

DSA Typescript
function diameterOfBinaryTree(root: TreeNode | null): number {
  let diameter = 0;
  function height(node: TreeNode | null): number {
    if (node === null) return 0;
    const leftHeight = height(node.left);
    const rightHeight = height(node.right);
    diameter = Math.max(diameter, [1] + [2]);
    return 1 + Math.max(leftHeight, rightHeight);
  }
  height(root);
  return diameter;
}
Drag options to blanks, or click blank then click option'
AleftHeight
BrightHeight
Cdiameter
D1
Attempts:
3 left
💡 Hint
Common Mistakes
Using diameter variable inside Math.max incorrectly.
Adding 1 to heights when updating diameter.
5fill in blank
hard

Fill all three blanks to complete the diameter calculation function with proper types and return.

DSA Typescript
function diameterOfBinaryTree(root: [1]): [2] {
  let diameter = 0;
  function height(node: [3]): number {
    if (node === null) return 0;
    const leftHeight = height(node.left);
    const rightHeight = height(node.right);
    diameter = Math.max(diameter, leftHeight + rightHeight);
    return 1 + Math.max(leftHeight, rightHeight);
  }
  height(root);
  return diameter;
}
Drag options to blanks, or click blank then click option'
ATreeNode | null
Bnumber
Dvoid
Attempts:
3 left
💡 Hint
Common Mistakes
Using void as return type instead of number.
Using TreeNode without null for parameters.