0
0
DSA Typescriptprogramming~10 mins

Tree Terminology Root Leaf Height Depth Level in DSA Typescript - Execution Trace

Choose your learning style9 modes available
Concept Flow - Tree Terminology Root Leaf Height Depth Level
Start at Root Node
Check if Node has Children
Yes No
Go to Child
Calculate Height
Calculate Depth
Assign Level
Repeat for all Nodes
End
This flow shows how to identify root, leaf nodes and calculate height, depth, and level for each node in a tree.
Execution Sample
DSA Typescript
class TreeNode {
  value: number;
  children: TreeNode[] = [];
  constructor(value: number) { this.value = value; }
}
Defines a simple tree node with a value and children array.
Execution Table
StepNodeIs Root?Is Leaf?DepthHeightLevel
1AYesNo020
2BNoNo111
3DNoYes202
4ENoYes202
5CNoYes101
6End-----
💡 All nodes processed; root identified; leaves found; depth, height, and level assigned.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 4After Step 5Final
NodeNoneABDECAll nodes visited
Is Root?N/AYesNoNoNoNoSet for all nodes
Is Leaf?N/ANoNoYesYesYesSet for all leaves
DepthN/A01221Assigned per node
HeightN/A21000Calculated bottom-up
LevelN/A01221Same as depth here
Key Moments - 3 Insights
Why is the root node's depth 0 but height 2?
Depth counts edges from root down, so root is 0. Height counts edges from node down to deepest leaf, so root has height 2 (see execution_table step 1).
How do we know a node is a leaf?
A leaf has no children. In the table, nodes D, E, and C have 'Is Leaf?' as Yes because they have no children (steps 3,4,5).
Are level and depth always the same?
In this example, yes. Level is often defined as depth from root, so they match here (see variable_tracker Level and Depth rows).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the height of node B at step 2?
A1
B0
C2
D3
💡 Hint
Check the 'Height' column for step 2 in the execution_table.
At which step does the node become identified as a leaf?
AStep 2
BStep 3
CStep 1
DStep 5
💡 Hint
Look for 'Is Leaf?' marked Yes in the execution_table.
If node C had children, how would its 'Is Leaf?' value change in the table?
AIt would stay Yes
BIt would become Root
CIt would change to No
DIt would have depth 0
💡 Hint
Leaves have no children, so adding children changes 'Is Leaf?' to No (see key_moments explanation).
Concept Snapshot
Tree Terminology Quick Reference:
- Root: top node, depth 0
- Leaf: node with no children
- Depth: edges from root to node
- Height: edges from node to deepest leaf
- Level: often same as depth
Use these to understand tree structure clearly.
Full Transcript
This visual execution traces tree terminology concepts. We start at the root node, which has depth 0 and height equal to the longest path to a leaf. Nodes with no children are leaves. Depth counts edges from root down, height counts edges from node down to deepest leaf. Level is often the same as depth. The execution table shows each node's properties step by step. Variable tracker records changes as we visit nodes. Key moments clarify common confusions like why root depth is zero but height is not, how leaves are identified, and the relation between level and depth. The quiz tests understanding by referencing the execution table and variable tracker. This helps beginners see how tree terms apply in practice.