0
0
DSA Typescriptprogramming~10 mins

Tree Traversal Preorder Root Left Right in DSA Typescript - Execution Trace

Choose your learning style9 modes available
Concept Flow - Tree Traversal Preorder Root Left Right
Start at Root Node
Visit Node: Process Data
Traverse Left Subtree
Traverse Right Subtree
Done
Start at the root, visit it, then recursively do the same for left subtree, then right subtree.
Execution Sample
DSA Typescript
function preorder(node) {
  if (!node) return;
  console.log(node.val);
  preorder(node.left);
  preorder(node.right);
}
Prints nodes in preorder: root first, then left subtree, then right subtree.
Execution Table
StepOperationNode VisitedLeft/RightTree State (Visual)
1Visit rootARootA ├─ B └─ C
2Traverse left subtreeBLeft of AA ├─ B │ ├─ D │ └─ E └─ C
3Traverse left subtreeDLeft of BA ├─ B │ ├─ D │ └─ E └─ C
4Left child nullnullLeft of DNo change
5Right child nullnullRight of DNo change
6Traverse right subtreeERight of BA ├─ B │ ├─ D │ └─ E └─ C
7Left child nullnullLeft of ENo change
8Right child nullnullRight of ENo change
9Traverse right subtreeCRight of AA ├─ B │ ├─ D │ └─ E └─ C └─ F
10Left child nullnullLeft of CNo change
11Traverse right subtreeFRight of CA ├─ B │ ├─ D │ └─ E └─ C └─ F
12Left child nullnullLeft of FNo change
13Right child nullnullRight of FNo change
14Traversal completenullNoneTraversal done
💡 All nodes visited in preorder: root, left subtree, right subtree.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 6After Step 9After Step 11Final
Current NodeAABDECFnull
Call Stack[][preorder(A)][preorder(B), preorder(A)][preorder(D), preorder(B), preorder(A)][preorder(E), preorder(B), preorder(A)][preorder(C), preorder(A)][preorder(F), preorder(C), preorder(A)][]
Output Sequence[][A][A, B][A, B, D][A, B, D, E][A, B, D, E, C][A, B, D, E, C, F][A, B, D, E, C, F]
Key Moments - 3 Insights
Why do we print the node value before traversing left and right children?
Because preorder traversal visits the root node first, then left subtree, then right subtree, as shown in execution_table steps 1, 2, and 9.
What happens when a node has no left or right child?
The traversal calls preorder with null, which returns immediately without action, as seen in steps 4, 5, 7, 8, 10, 12, and 13.
How does the call stack change during traversal?
Each recursive call adds a function to the stack; when a node is fully processed, its call returns and is removed, shown in variable_tracker's Call Stack column.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 3, which node is currently visited?
ANode E
BNode B
CNode D
DNode C
💡 Hint
Check the 'Node Visited' column at step 3 in execution_table.
At which step does the traversal visit the right child of node A?
AStep 2
BStep 9
CStep 6
DStep 11
💡 Hint
Look for 'Traverse right subtree' operation with node C in execution_table.
If node B had no left child, which step would be skipped?
AStep 4
BStep 6
CStep 3
DStep 7
💡 Hint
Step 4 is visiting left child null of D; if no left child of B, step 4 would be skipped.
Concept Snapshot
Preorder Tree Traversal:
- Visit root node first
- Recursively traverse left subtree
- Recursively traverse right subtree
- Uses recursion and call stack
- Output order: Root, Left, Right
Full Transcript
Preorder traversal visits nodes starting from the root, then left subtree, then right subtree. The code recursively visits each node, printing its value before going deeper. When a node is null, the recursion returns immediately. The call stack grows with each recursive call and shrinks as calls return. The output sequence follows the order nodes are visited.