0
0
DSA Typescriptprogramming~20 mins

DP on Trees Diameter of Tree in DSA Typescript - Practice Problems & Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Tree 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 tree
What is the output of the following TypeScript code that calculates the diameter of a tree using DP on trees?
DSA Typescript
type Tree = { [key: number]: number[] };

function treeDiameter(tree: Tree): number {
  let diameter = 0;

  function dfs(node: number, parent: number): number {
    let max1 = 0, max2 = 0;
    for (const child of tree[node] || []) {
      if (child === parent) continue;
      const depth = dfs(child, node);
      if (depth > max1) {
        max2 = max1;
        max1 = depth;
      } else if (depth > max2) {
        max2 = depth;
      }
    }
    diameter = Math.max(diameter, max1 + max2);
    return max1 + 1;
  }

  dfs(0, -1);
  return diameter;
}

const tree = {
  0: [1, 2],
  1: [0, 3, 4],
  2: [0],
  3: [1],
  4: [1]
};

console.log(treeDiameter(tree));
A2
B4
C3
D5
Attempts:
2 left
💡 Hint
Think about the longest path between any two nodes in the tree.
🧠 Conceptual
intermediate
1:30remaining
Understanding the role of DP in tree diameter calculation
Which statement best describes how dynamic programming (DP) is used in calculating the diameter of a tree?
ADP is used to count the total number of nodes in the tree.
BDP stores the longest path from each node to its descendants to avoid recomputation.
CDP helps in converting the tree into a binary tree for easier diameter calculation.
DDP is used to sort the nodes in the tree before calculating the diameter.
Attempts:
2 left
💡 Hint
Think about how repeated calculations are avoided in DFS.
🔧 Debug
advanced
2:00remaining
Identify the error in this diameter calculation code
What error will this TypeScript code produce when calculating the diameter of a tree?
DSA Typescript
type Tree = { [key: number]: number[] };

function treeDiameter(tree: Tree): number {
  let diameter = 0;

  function dfs(node: number, parent: number): number {
    let max1 = 0, max2 = 0;
    for (const child of tree[node] || []) {
      if (child === parent) continue;
      const depth = dfs(child, node);
      if (depth > max1) {
        max2 = max1;
        max1 = depth;
      } else if (depth > max2) {
        max2 = depth;
      }
    }
    diameter = Math.max(diameter, max1 + max2);
    return max1 + 1;
  }

  dfs(0, -1);
  return diameter;
}

const tree = {
  0: [1, 2],
  1: [0, 3, 4]
};

console.log(treeDiameter(tree));
ATypeError because tree[node] can be undefined
BSyntaxError due to missing semicolon
CRuntime error due to infinite recursion
DNo error, outputs correct diameter
Attempts:
2 left
💡 Hint
Check if all nodes have entries in the tree object.
🚀 Application
advanced
1:30remaining
Calculate diameter for a star-shaped tree
Given a star-shaped tree with 1 center node connected to 5 leaf nodes, what is the diameter of this tree?
A6
B5
C1
D2
Attempts:
2 left
💡 Hint
The diameter is the longest path between two leaves through the center.
Predict Output
expert
2:30remaining
Output of diameter calculation on a complex tree
What is the output of this TypeScript code that calculates the diameter of a more complex tree?
DSA Typescript
type Tree = { [key: number]: number[] };

function treeDiameter(tree: Tree): number {
  let diameter = 0;

  function dfs(node: number, parent: number): number {
    let max1 = 0, max2 = 0;
    for (const child of tree[node] || []) {
      if (child === parent) continue;
      const depth = dfs(child, node);
      if (depth > max1) {
        max2 = max1;
        max1 = depth;
      } else if (depth > max2) {
        max2 = depth;
      }
    }
    diameter = Math.max(diameter, max1 + max2);
    return max1 + 1;
  }

  dfs(0, -1);
  return diameter;
}

const tree = {
  0: [1, 2],
  1: [0, 3, 4],
  2: [0, 5],
  3: [1, 6],
  4: [1],
  5: [2, 7, 8],
  6: [3],
  7: [5],
  8: [5]
};

console.log(treeDiameter(tree));
A6
B7
C5
D8
Attempts:
2 left
💡 Hint
Find the longest path between any two nodes in the tree.