📖 Scenario: Imagine you have a family tree and you want to visit each family member starting from the oldest ancestor, then their children, and so on. This is like walking through a tree data structure in a specific order called preorder traversal.
🎯 Goal: You will build a simple tree structure in Go and write code to visit each node in preorder: first the root, then the left child, then the right child. Finally, you will print the values in the order they are visited.
📋 What You'll Learn
Create a tree node struct with integer values and left/right children
Build a small tree with exactly 3 nodes: root, left child, right child
Write a recursive function called preorder that visits nodes in root-left-right order
Print the values of nodes visited by preorder traversal separated by spaces
💡 Why This Matters
🌍 Real World
Tree traversal is used in many applications like file system navigation, expression evaluation, and organizing hierarchical data.
💼 Career
Understanding tree traversal is essential for software engineers working with data structures, algorithms, and system design.
Progress0 / 4 steps
1
Create the Tree Node Structure and Build the Tree
Create a struct called Node with an int field called Value and two pointers to Node called Left and Right. Then create three variables: root with value 1, leftChild with value 2, and rightChild with value 3. Connect root.Left to leftChild and root.Right to rightChild.
DSA Go
Hint
Define the Node struct first. Then create pointers to nodes for root, leftChild, and rightChild. Finally, link the children to the root.
2
Add a Slice to Collect Visited Node Values
Create a variable called result of type []int to store the values of nodes visited during preorder traversal.
DSA Go
Hint
Use result := []int{} to create an empty slice of integers.
3
Write the Preorder Traversal Function
Write a recursive function called preorder that takes a pointer to Node and a pointer to []int. It should visit the current node first, append its Value to result, then recursively call itself on the Left child, then on the Right child. Call preorder with root and &result inside main.
DSA Go
Hint
Check if the node is nil to stop recursion. Append the node's value to the slice. Then call preorder on left and right children.
4
Print the Preorder Traversal Result
Print the values stored in result separated by spaces using a for loop inside main.
DSA Go
Hint
Use a for loop to print each value. Print a space before each value except the first. End with a newline.
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.