0
0
DSA Javascriptprogramming~10 mins

Height of Binary Tree in DSA Javascript - Execution Trace

Choose your learning style9 modes available
Concept Flow - Height of Binary Tree
Start at root node
Is node null?
YesReturn 0
No
Find height of left subtree
Find height of right subtree
Height = max(left, right) + 1
Return height
The height is found by recursively checking left and right subtrees, then taking the larger height plus one.
Execution Sample
DSA Javascript
function height(node) {
  if (node === null) return 0;
  let leftHeight = height(node.left);
  let rightHeight = height(node.right);
  return Math.max(leftHeight, rightHeight) + 1;
}
This function calculates the height of a binary tree by recursively finding the height of left and right children.
Execution Table
StepOperationNode VisitedLeft HeightRight HeightHeight ComputedTree State
1Start at root1---1 / \ 2 3 / \ 4 5
2Go left2---Same as above
3Go left4---Same as above
4Check left childnull--0Leaf reached
5Check right childnull--0Leaf reached
6Compute height4001Node 4 height = 1
7Go right5---Node 5 leaf
8Check left childnull--0Leaf reached
9Check right childnull--0Leaf reached
10Compute height5001Node 5 height = 1
11Compute height2112Node 2 height = 2
12Go right3---Node 3 leaf
13Check left childnull--0Leaf reached
14Check right childnull--0Leaf reached
15Compute height3001Node 3 height = 1
16Compute height1213Root node height = 3
💡 All nodes visited, height computed from bottom up
Variable Tracker
VariableStartAfter Step 6After Step 10After Step 11After Step 15After Step 16
node1 (root)4 (left leaf)5 (right leaf of 2)2 (left child of root)3 (right child of root)1 (root)
leftHeight-00102
rightHeight-00101
height-11213
Key Moments - 3 Insights
Why do we return 0 when the node is null?
Returning 0 for null nodes means leaf nodes have height 1 (0 + 1). This is shown in steps 4, 5, 8, 9, 13, and 14 where null children return 0.
Why do we add 1 after taking max of left and right heights?
Adding 1 accounts for the current node's level. Steps 6, 10, 11, 15, and 16 show height computed as max(leftHeight, rightHeight) + 1.
How does recursion ensure all nodes are visited?
The function calls itself on left and right children until null is reached (leaf), ensuring bottom-up height calculation as seen in the step sequence.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the height computed at step 11 for node 2?
A1
B3
C2
D0
💡 Hint
Check the 'Height Computed' column at step 11 in the execution table.
At which step does the function return height 1 for node 5?
AStep 10
BStep 6
CStep 15
DStep 16
💡 Hint
Look for node 5 in the 'Node Visited' column and check the height computed.
If the left subtree of node 2 was taller by 1, how would the height at root (step 16) change?
AIt would remain 3
BIt would become 4
CIt would become 2
DIt would become 5
💡 Hint
Height at root is max(leftHeight, rightHeight) + 1; increasing leftHeight by 1 increases root height by 1.
Concept Snapshot
Height of Binary Tree:
- Base case: null node height = 0
- Recursively find left and right subtree heights
- Height = max(leftHeight, rightHeight) + 1
- Returns height of tree from bottom up
- Used to measure tree depth
Full Transcript
The height of a binary tree is found by starting at the root and recursively checking the height of left and right children. If a node is null, it returns 0, meaning leaf nodes have height 1. For each node, the height is the maximum height of its left and right subtrees plus one for the current node. This process continues until all nodes are visited and the height of the root node is computed. The execution table shows each step visiting nodes, checking children, and computing heights. The variable tracker follows the node visited and height values at key steps. Key moments clarify why null returns 0 and why we add 1 after max. The visual quiz tests understanding of height values at specific steps and how changes affect the root height.