0
0
DSA Javascriptprogramming~10 mins

Diameter of Binary Tree in DSA Javascript - 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
Return height of current node = max(left height, right height) + 1
Repeat for left and right child nodes recursively
End when all nodes visited
Final max diameter is result
We start at the root and recursively find the height of left and right subtrees. At each node, we calculate the diameter passing through it and update the maximum diameter found. We return the height to parent nodes to continue the process.
Execution Sample
DSA Javascript
function diameterOfBinaryTree(root) {
  let maxDiameter = 0;
  function height(node) {
    if (!node) return 0;
    let left = height(node.left);
    let right = height(node.right);
    maxDiameter = Math.max(maxDiameter, left + right);
    return Math.max(left, right) + 1;
  }
  height(root);
  return maxDiameter;
}
This code calculates the diameter of a binary tree by recursively computing subtree heights and updating the maximum diameter found.
Execution Table
StepOperationNode VisitedLeft HeightRight HeightDiameter Through NodeMax DiameterReturn HeightTree State
1Visit root1???0?1
2Visit left child2???0?1 -> 2
3Visit left-left child4000011 -> 2 -> 4
4Return from node 44000011 -> 2 -> 4
5Visit left-right child5000011 -> 2 -> 4,5
6Return from node 55000011 -> 2 -> 4,5
7Calculate node 2 diameter2112221 -> 2 -> 4,5
8Return from node 22112221 -> 2 -> 4,5
9Visit right child3???2?1 -> 2,3 -> 4,5
10Visit right-left child6000211 -> 2,3 -> 4,5,6
11Return from node 66000211 -> 2,3 -> 4,5,6
12Visit right-right child7000211 -> 2,3 -> 4,5,6,7
13Return from node 77000211 -> 2,3 -> 4,5,6,7
14Calculate node 3 diameter3112221 -> 2,3 -> 4,5,6,7
15Return from node 33112221 -> 2,3 -> 4,5,6,7
16Calculate root node diameter1224431 -> 2,3 -> 4,5,6,7
17Return from root1224431 -> 2,3 -> 4,5,6,7
18End----4-Final diameter = 4
💡 All nodes visited, maxDiameter updated to 4, recursion ends.
Variable Tracker
VariableStartAfter Step 3After Step 6After Step 7After Step 11After Step 13After Step 14After Step 16Final
maxDiameter000222244
height(node 4)?11111111
height(node 5)??1111111
height(node 2)???222222
height(node 6)????11111
height(node 7)?????1111
height(node 3)??????222
height(node 1)???????33
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, which can pass through the current node connecting left and right subtrees. This is shown in execution_table rows 7, 14, and 16 where diameter through node is left height + right height.
Why do we return max(left height, right height) + 1 as height?
Height is the longest path from the node down to a leaf. We add 1 for the current node itself. This is tracked in variable_tracker for each node's height after visiting children.
Why does maxDiameter update only when left + right is larger?
Because diameter is the longest path found so far. We update maxDiameter only if the current node's diameter (left + right) is greater, as seen in execution_table steps 7, 14, and 16.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 7, what is the diameter through node 2?
A1
B2
C3
D0
💡 Hint
Check the 'Diameter Through Node' column at step 7 in execution_table.
At which step does maxDiameter first update to 2?
AStep 7
BStep 3
CStep 6
DStep 14
💡 Hint
Look at the 'Max Diameter' column in execution_table and find when it changes from 0 to 2.
If the tree had no right subtree, how would maxDiameter change at root?
AIt would be left subtree height + 1
BIt would be equal to left subtree height
CIt would be left subtree height
DIt would be 0
💡 Hint
Diameter through node is left height + right height. If right height is 0, diameter equals left height.
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 height = max(left, right) + 1
- Final max diameter is the answer
Full Transcript
The diameter of a binary tree is the longest path between any two nodes. We find it by visiting each node and calculating the height of its left and right subtrees. The diameter through a node is the sum of these heights. We keep track of the maximum diameter found so far. The height returned to parent nodes is the maximum of left and right heights plus one for the current node. This process repeats recursively until all nodes are visited. The final maximum diameter is returned as the result.