0
0
DSA Typescriptprogramming~10 mins

Diameter of Binary Tree in DSA Typescript - Execution Trace

Choose your learning style9 modes available
Concept Flow - Diameter of Binary Tree
Start at root node
Calculate left subtree height
Calculate right subtree height
Calculate diameter through current node = left height + right height
Update max diameter if current diameter is larger
Recursively repeat for left child
Recursively repeat for right child
Return max diameter found
We find the height of left and right subtrees at each node, calculate diameter through that node, update max diameter, and recurse for all nodes.
Execution Sample
DSA Typescript
function diameterOfBinaryTree(root: TreeNode | null): number {
  let maxDiameter = 0;
  function height(node: TreeNode | null): number {
    if (!node) return 0;
    const left = height(node.left);
    const right = height(node.right);
    maxDiameter = Math.max(maxDiameter, left + right);
    return 1 + Math.max(left, right);
  }
  height(root);
  return maxDiameter;
}
Calculates the diameter of a binary tree by recursively computing subtree heights and updating max diameter.
Execution Table
StepOperationNode VisitedLeft HeightRight HeightDiameter Through NodeMax DiameterVisual Tree State
1Visit node1 (root)???01 / \ 2 3
2Visit node2 (left child)???01 / \ 2 3
3Visit node4 (left child of 2)00001 / \ 2 3 / 4
4Return height400001 / \ 2 3 / 4
5Visit node5 (right child of 2)00001 / \ 2 3 / \ 4 5
6Return height500001 / \ 2 3 / \ 4 5
7Calculate heights211221 / \ 2 3 / \ 4 5
8Return height211221 / \ 2 3 / \ 4 5
9Visit node3 (right child)???21 / \ 2 3 / \ 4 5
10Visit node6 (left child of 3)00021 / \ 2 3 / \ 4 5 / 6
11Return height600021 / \ 2 3 / \ 4 5 / 6
12Visit node7 (right child of 3)00021 / \ 2 3 / \ 4 5 / \ 6 7
13Return height700021 / \ 2 3 / \ 4 5 / \ 6 7
14Calculate heights311221 / \ 2 3 / \ 4 5 / \ 6 7
15Return height311221 / \ 2 3 / \ 4 5 / \ 6 7
16Calculate heights1 (root)22441 / \ 2 3 / \ 4 5 / \ 6 7
17Return height1 (root)22441 / \ 2 3 / \ 4 5 / \ 6 7
18End----4Final max diameter is 4
💡 All nodes visited, max diameter updated to 4, recursion ends
Variable Tracker
VariableStartAfter Step 4After Step 6After Step 8After Step 11After Step 13After Step 15After Step 17Final
maxDiameter000222244
height(4)N/A11111111
height(5)N/AN/A1111111
height(2)N/AN/AN/A222222
height(6)N/AN/AN/AN/A11111
height(7)N/AN/AN/AN/AN/A1111
height(3)N/AN/AN/AN/AN/AN/A222
height(1)N/AN/AN/AN/AN/AN/AN/A33
Key Moments - 3 Insights
Why do we add left height and right height to get diameter at a node?
Because diameter is the longest path between two nodes, passing through current node means path goes down left subtree and right subtree, so sum of left and right heights gives that path length (see step 7 and 14).
Why do we update maxDiameter inside the height function?
Because at each node we calculate diameter through it and compare with maxDiameter so far, updating maxDiameter ensures we keep track of the largest diameter found during recursion (see steps 7, 14, and 16).
Why does height return 1 + max(left, right)?
Height counts edges down to leaf plus current node, so 1 for current node plus the taller subtree height below it (see steps 8, 15, 17).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table at step 16, what is the diameter through the root node?
A4
B2
C3
D0
💡 Hint
Check the 'Diameter Through Node' column at step 16 in the execution_table.
At which step does maxDiameter first update from 0 to 2?
AStep 14
BStep 4
CStep 7
DStep 16
💡 Hint
Look at the 'Max Diameter' column in execution_table rows to find when it changes from 0 to 2.
If node 5 had a left child, how would maxDiameter change at step 7?
AIt would stay the same
BIt would increase
CIt would decrease
DIt would become zero
💡 Hint
Adding a child increases subtree height, which increases diameter through node (see how diameter is left + right heights).
Concept Snapshot
Diameter of Binary Tree:
- Diameter = longest path between any two nodes
- At each node, diameter = left subtree height + right subtree height
- Use recursion to get heights and update max diameter
- Return max diameter after full traversal
- Height(node) = 1 + max(height(left), height(right))
Full Transcript
The diameter of a binary tree is the longest path between any two nodes. To find it, we visit each node and calculate the height of its left and right subtrees. The diameter through that node is the sum of these two heights. We keep track of the maximum diameter found so far. We use a recursive function to compute heights and update the maximum diameter. Finally, we return the maximum diameter after visiting all nodes. The height of a node is 1 plus the maximum height of its children. This approach ensures we consider all possible paths in the tree.