0
0
DSA Typescriptprogramming~10 mins

Height of Binary Tree in DSA Typescript - 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
We start at the root and recursively find the height of left and right subtrees. The height of the node is the maximum of these two heights plus one.
Execution Sample
DSA Typescript
function height(node: TreeNode | null): number {
  if (node === null) return 0;
  const leftHeight = height(node.left);
  const 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 and returning the maximum plus one.
Execution Table
StepOperationNode VisitedLeft HeightRight HeightHeight ComputedTree State
1Start at rootA---A / \ B C / / \ D E F
2Go leftB---B / D
3Go leftD---D (leaf)
4Left child nullnull0---
5Right child nullnull-0--
6Compute height at DD001D (height=1)
7Right child null of Bnull-0--
8Compute height at BB102B (height=2)
9Go rightC---C / \ E F
10Go leftE---E (leaf)
11Left child nullnull0---
12Right child nullnull-0--
13Compute height at EE001E (height=1)
14Go rightF---F (leaf)
15Left child nullnull0---
16Right child nullnull-0--
17Compute height at FF001F (height=1)
18Compute height at CC112C (height=2)
19Compute height at AA223A (height=3)
20Return final heightA223Tree height = 3
💡 All nodes visited, heights computed bottom-up, final height returned at root.
Variable Tracker
VariableStartAfter Step 6After Step 8After Step 13After Step 17After Step 18Final
nodeADBEFCA
leftHeight-010012
rightHeight-000012
height-121123
Key Moments - 3 Insights
Why do we return 0 when the node is null?
Returning 0 for a null node means the height of an empty subtree is zero, as shown in steps 4, 5, 11, 12, 15, and 16 in the execution_table.
Why do we add 1 after taking the max of left and right heights?
Adding 1 accounts for the current node's level, as seen in steps 6, 8, 13, 17, 18, and 19 where height is max(leftHeight, rightHeight) + 1.
How does the recursion ensure all nodes are visited?
The recursion visits left and right children before computing height at the current node, demonstrated by the bottom-up order in the execution_table from leaves (D, E, F) to root (A).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the height computed at node B (step 8)?
A1
B2
C3
D0
💡 Hint
Check the 'Height Computed' column at step 8 in the execution_table.
At which step does the recursion return height 1 for leaf node E?
AStep 11
BStep 17
CStep 13
DStep 6
💡 Hint
Look for node E and height 1 in the execution_table rows.
If node F had a left child, how would the final height of the tree change?
AIt would increase by 1
BIt would stay the same
CIt would decrease
DIt would become 0
💡 Hint
Consider how adding a child increases subtree height, affecting the max height at ancestors.
Concept Snapshot
Height of Binary Tree:
- Height is max height of left and right subtree + 1
- Null node height = 0
- Use recursion to find left and right heights
- Return max(leftHeight, rightHeight) + 1
- Base case: node == null returns 0
Full Transcript
The height of a binary tree is found by starting at the root and recursively finding the height of its left and right subtrees. If a node is null, the height is zero. For each node, the height is the maximum of the left and right subtree heights plus one for the current node. This process continues bottom-up until the root's height is computed. The example tree has height 3, as shown by the step-by-step execution where leaf nodes have height 1, their parents have height 2, and the root has height 3.