0
0
DSA Goprogramming~10 mins

Height of Binary Tree in DSA Go - 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
Compare left and right heights
Height = max(left, right) + 1
Return height
Start from the root, recursively find heights of left and right subtrees, then return the larger height plus one.
Execution Sample
DSA Go
func height(node *Node) int {
  if node == nil {
    return 0
  }
  leftHeight := height(node.Left)
  rightHeight := height(node.Right)
  if leftHeight > rightHeight {
    return leftHeight + 1
  }
  return rightHeight + 1
}
This function calculates the height of a binary tree by recursively checking left and right subtree heights.
Execution Table
StepOperationNode VisitedLeft HeightRight HeightHeight ComputedTree State
1Start at root10---10 / \ 5 15 / \ \ 3 7 20
2Go left5---Subtree rooted at 5
3Go left3---Leaf node 3
4Node 3 is leaf30013
5Go right7---Leaf node 7
6Node 7 is leaf70017
7Compute height at 551125 / \ 3 7
8Go right15---Subtree rooted at 15
9Go leftnil---Empty subtree
10Left height at 15nil0---
11Go right20---Leaf node 20
12Node 20 is leaf2000120
13Compute height at 151501215 \ 20
14Compute height at root 101022310 / \ 5 15 / \ \ 3 7 20
15Return final height---3Complete tree height is 3
💡 All nodes visited, heights computed bottom-up, final height returned.
Variable Tracker
VariableStartAfter Step 4After Step 6After Step 7After Step 12After Step 13After Step 14Final
node10375201510-
leftHeight-001002-
rightHeight-001012-
height-1121233
Key Moments - 3 Insights
Why do we return 0 when the node is nil?
Because a nil node means no tree exists there, so its height is 0. This is shown in steps 3, 9, and 10 where nil nodes return 0 height.
Why do we add 1 after taking max of left and right heights?
We add 1 to count the current node itself. Steps 7, 13, and 14 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 it reaches nil nodes (steps 3, 9). This bottom-up approach ensures every node's height is computed.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table at step 7, what is the height computed for node 5?
A1
B2
C3
D0
💡 Hint
Check the 'Height Computed' column at step 7 in the execution table.
At which step does the function return height for the leaf node 20?
AStep 6
BStep 12
CStep 14
DStep 4
💡 Hint
Look for the step where node 20 is visited and height is computed in the execution table.
If the right subtree of node 15 was empty, what would be the height computed at node 15?
A1
B0
C2
D3
💡 Hint
Refer to steps 10 and 13 where left or right subtree heights are 0 and how height is computed.
Concept Snapshot
Height of Binary Tree:
- Base case: nil node height = 0
- Recursively find left and right subtree heights
- Height = max(leftHeight, rightHeight) + 1
- Counts edges from root to deepest leaf
- Uses post-order traversal (bottom-up)
Full Transcript
To find the height of a binary tree, start at the root node. If the node is nil, return 0 because there is no tree there. Otherwise, recursively find the height of the left subtree and the right subtree. Then, take the maximum of these two heights and add 1 to count the current node. This process continues until all nodes are visited and their heights computed. The final returned value is the height of the entire tree. This method uses a bottom-up approach, visiting leaf nodes first and moving up to the root.