This code prints nodes in preorder: root first, then left subtree, then right subtree.
Execution Table
Step
Operation
Node Visited
Pointer Changes
Tree State (Visual)
1
Visit root
A
current = A
A
├─ B
└─ C
2
Traverse left subtree
B
current = B
A
├─ B
└─ C
3
Visit node
B
none
A
├─ B
└─ C
4
Traverse left subtree
D
current = D
A
├─ B
│ ├─ D
│ └─ E
└─ C
5
Visit node
D
none
A
├─ B
│ ├─ D
│ └─ E
└─ C
6
Traverse left subtree
nil
current = nil
A
├─ B
│ ├─ D
│ └─ E
└─ C
7
Return from nil
nil
back to D
A
├─ B
│ ├─ D
│ └─ E
└─ C
8
Traverse right subtree
nil
current = nil
A
├─ B
│ ├─ D
│ └─ E
└─ C
9
Return from nil
nil
back to B
A
├─ B
│ ├─ D
│ └─ E
└─ C
10
Traverse right subtree
E
current = E
A
├─ B
│ ├─ D
│ └─ E
└─ C
11
Visit node
E
none
A
├─ B
│ ├─ D
│ └─ E
└─ C
12
Traverse left subtree
nil
current = nil
A
├─ B
│ ├─ D
│ └─ E
└─ C
13
Return from nil
nil
back to E
A
├─ B
│ ├─ D
│ └─ E
└─ C
14
Traverse right subtree
nil
current = nil
A
├─ B
│ ├─ D
│ └─ E
└─ C
15
Return from nil
nil
back to B
A
├─ B
│ ├─ D
│ └─ E
└─ C
16
Return from B
B
back to A
A
├─ B
│ ├─ D
│ └─ E
└─ C
17
Traverse right subtree
C
current = C
A
├─ B
│ ├─ D
│ └─ E
└─ C
├─ F
└─ G
18
Visit node
C
none
A
├─ B
│ ├─ D
│ └─ E
└─ C
├─ F
└─ G
19
Traverse left subtree
F
current = F
A
├─ B
│ ├─ D
│ └─ E
└─ C
├─ F
└─ G
20
Visit node
F
none
A
├─ B
│ ├─ D
│ └─ E
└─ C
├─ F
└─ G
21
Traverse left subtree
nil
current = nil
A
├─ B
│ ├─ D
│ └─ E
└─ C
├─ F
└─ G
22
Return from nil
nil
back to F
A
├─ B
│ ├─ D
│ └─ E
└─ C
├─ F
└─ G
23
Traverse right subtree
nil
current = nil
A
├─ B
│ ├─ D
│ └─ E
└─ C
├─ F
└─ G
24
Return from nil
nil
back to C
A
├─ B
│ ├─ D
│ └─ E
└─ C
├─ F
└─ G
25
Traverse right subtree
G
current = G
A
├─ B
│ ├─ D
│ └─ E
└─ C
├─ F
└─ G
26
Visit node
G
none
A
├─ B
│ ├─ D
│ └─ E
└─ C
├─ F
└─ G
27
Traverse left subtree
nil
current = nil
A
├─ B
│ ├─ D
│ └─ E
└─ C
├─ F
└─ G
28
Return from nil
nil
back to G
A
├─ B
│ ├─ D
│ └─ E
└─ C
├─ F
└─ G
29
Traverse right subtree
nil
current = nil
A
├─ B
│ ├─ D
│ └─ E
└─ C
├─ F
└─ G
30
Return from nil
nil
back to C
A
├─ B
│ ├─ D
│ └─ E
└─ C
├─ F
└─ G
31
Return from C
C
back to A
A
├─ B
│ ├─ D
│ └─ E
└─ C
├─ F
└─ G
32
Return from root
A
Traversal complete
A
├─ B
│ ├─ D
│ └─ E
└─ C
├─ F
└─ G
💡 Traversal ends after visiting all nodes in preorder: A, B, D, E, C, F, G
Variable Tracker
Variable
Start
After Step 1
After Step 4
After Step 10
After Step 17
After Step 25
Final
current
nil
A
D
E
C
G
nil
visited_nodes
[]
[A]
[A, B, D]
[A, B, D, E]
[A, B, D, E, C]
[A, B, D, E, C, F, G]
[A, B, D, E, C, F, G]
Key Moments - 3 Insights
Why do we visit the node before traversing its left and right children?
Because preorder means Root-Left-Right, so visiting the node first is shown in execution_table rows 1, 3, 5, 11, 18, 20, 26 where the node is printed before moving to children.
What happens when the traversal reaches a nil (empty) child?
The function returns immediately without visiting, as shown in steps 6, 8, 12, 14, 21, 23, 27, 29 where current becomes nil and returns back to parent node.
How does the traversal know when to return to the parent node?
When both left and right children are nil or fully visited, the recursion returns to the previous call, as seen in steps 9, 15, 16, 24, 30, 31, and finally 32 when traversal ends.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what node is visited at step 10?
AE
BD
CB
DC
💡 Hint
Check the 'Node Visited' column at step 10 in execution_table.
At which step does the traversal first return from a nil child?
AStep 15
BStep 9
CStep 6
DStep 21
💡 Hint
Look for 'Return from nil' in the 'Operation' column in execution_table.
If the left child of node B was missing, how would the visited_nodes list change after step 10?
A[A, B, D, E]
B[A, B, E]
C[A, B]
D[A, B, D]
💡 Hint
Refer to variable_tracker's 'visited_nodes' and consider skipping node D.
Concept Snapshot
Preorder Traversal visits nodes in Root-Left-Right order.
Start at root, visit node, then recursively traverse left subtree, then right subtree.
If node is nil, return immediately.
Useful for copying trees or prefix expressions.
Output order example: A, B, D, E, C, F, G.
Full Transcript
Preorder tree traversal means visiting the root node first, then the left subtree, and finally the right subtree. The code visits a node by printing its value, then recursively calls itself on the left child, then on the right child. When a nil child is reached, the function returns immediately. The traversal ends after all nodes are visited in this order. This method is useful for tasks like copying a tree or evaluating prefix expressions.
Practice
(1/5)
1. What is the order of nodes visited in a preorder tree traversal?
easy
A. Root, Left, Right
B. Left, Root, Right
C. Left, Right, Root
D. Right, Left, Root
Solution
Step 1: Understand preorder traversal definition
Preorder traversal visits the root node first, then the left subtree, and finally the right subtree.
Step 2: Match the order with options
Root, Left, Right matches the order Root, Left, Right exactly.
Final Answer:
Root, Left, Right -> Option A
Quick Check:
Preorder = Root, Left, Right [OK]
Hint: Remember preorder starts with root node first [OK]
Common Mistakes:
Confusing preorder with inorder or postorder
Swapping left and right subtree order
Starting traversal from left child instead of root
2. Which of the following Go function signatures correctly represents a preorder traversal function for a binary tree node Node?
easy
A. func preorder(root *Node) { if root == nil { return } preorder(root.Left) preorder(root.Right) fmt.Println(root.Value) }
B. func preorder(root *Node) { if root == nil { return } fmt.Println(root.Value) preorder(root.Left) preorder(root.Right) }
C. func preorder(root *Node) { if root == nil { return } preorder(root.Right) fmt.Println(root.Value) preorder(root.Left) }
D. func preorder(root *Node) { if root == nil { return } preorder(root.Left) fmt.Println(root.Value) preorder(root.Right) }
Solution
Step 1: Recall preorder traversal order
Preorder visits root first, then left subtree, then right subtree.
Step 2: Check function order
func preorder(root *Node) { if root == nil { return } fmt.Println(root.Value) preorder(root.Left) preorder(root.Right) } prints root value first, then recursively visits left and right children, matching preorder.
Final Answer:
func preorder(root *Node) { if root == nil { return } fmt.Println(root.Value) preorder(root.Left) preorder(root.Right) } -> Option B
Quick Check:
Print root before children calls [OK]
Hint: Print root value before recursive calls [OK]
Common Mistakes:
Printing after recursive calls (postorder)
Swapping left and right recursive calls
Missing nil check for root
3. Given the binary tree below, what is the preorder traversal output?
A. The function uses fmt.Print instead of fmt.Println
B. The function does not check for nil root
C. The root value is printed after left subtree traversal, not before
D. The right subtree is traversed before the left subtree
Solution
Step 1: Analyze print position in code
The root value is printed after preorder(root.Left), so root is visited after left subtree.
Step 2: Compare with preorder definition
Preorder requires visiting root first, then left, then right. This code visits left first, then root, then right (inorder).
Final Answer:
The root value is printed after left subtree traversal, not before -> Option C
Quick Check:
Print root before children for preorder [OK]
Hint: Print root value before recursive calls to fix order [OK]
Common Mistakes:
Confusing preorder with inorder traversal
Ignoring nil check (though present here)
Thinking print function choice affects traversal order
5. You want to collect all node values of a binary tree in preorder traversal into a slice in Go. Which code snippet correctly does this?
hard
A. func preorder(root *Node, result *[]int) { if root == nil { return } *result = append(*result, root.Value) preorder(root.Left, result) preorder(root.Right, result) }
B. func preorder(root *Node, result []int) []int { if root == nil { return result } result = append(result, root.Value) preorder(root.Left, result) preorder(root.Right, result) return result }
C. func preorder(root *Node) []int { if root == nil { return nil } left := preorder(root.Left) right := preorder(root.Right) return append(left, append(right, root.Value)...) }
D. func preorder(root *Node, result *[]int) { if root == nil { return } preorder(root.Left, result) preorder(root.Right, result) *result = append(*result, root.Value) }
Solution
Step 1: Understand preorder collection logic
Preorder means add root value first, then traverse left and right subtrees.
Step 2: Check each option's order and parameter usage
func preorder(root *Node, result *[]int) { if root == nil { return } *result = append(*result, root.Value) preorder(root.Left, result) preorder(root.Right, result) } appends root value first, then recursively traverses left and right, using pointer to slice to collect values correctly.
Final Answer:
func preorder(root *Node, result *[]int) { if root == nil { return } *result = append(*result, root.Value) preorder(root.Left, result) preorder(root.Right, result) } -> Option A
Quick Check:
Append root first, use pointer to slice [OK]
Hint: Append root value before recursive calls, pass slice pointer [OK]
Common Mistakes:
Appending after recursive calls (wrong order)
Passing slice by value and expecting changes
Appending right subtree before left subtree
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.