📖 Scenario: You are working with a simple family tree where each person can have up to two children. You want to visit all family members in a special order: first the left child, then the right child, and finally the parent. This order is called postorder traversal.
🎯 Goal: Build a program that creates a small tree of family members, sets up a function to visit them in postorder (left, right, root), and prints the names in that order.
📋 What You'll Learn
Create a tree node structure with a name string and pointers to left and right children
Build a small tree with exactly these nodes and connections:
"Grandpa" as root, with "Dad" as left child and "Uncle" as right child
"Dad" has "Me" as left child and "Sister" as right child
Write a recursive function called postorder that visits nodes in left, right, root order and collects their names
Print the names in the order they are visited separated by spaces
💡 Why This Matters
🌍 Real World
Tree traversal is used in many areas like file system navigation, organizing family trees, and processing expressions in calculators.
💼 Career
Understanding tree traversal helps in software development roles involving data structures, algorithms, and system design.
Progress0 / 4 steps
1
Create the tree nodes
Create variables called grandpa, dad, uncle, me, and sister of type *Node. Initialize each with the name field set to exactly "Grandpa", "Dad", "Uncle", "Me", and "Sister" respectively. The left and right fields should be nil for now.
DSA Go
Hint
Use &Node{name: "Name"} to create each node. Set left and right to nil by default.
2
Connect the nodes to form the tree
Set the left and right fields to connect the nodes as follows: grandpa.left = dad, grandpa.right = uncle, dad.left = me, and dad.right = sister.
DSA Go
Hint
Assign the child nodes to the left and right fields of their parents.
3
Write the postorder traversal function
Write a recursive function called postorder that takes a *Node and a pointer to a []string. It should visit the left child, then the right child, then add the current node's name to the slice.
DSA Go
Hint
Check if node is nil to stop. Then call postorder on left and right. Finally, add node.name to *names.
4
Print the postorder traversal result
Create a slice of strings called names. Call postorder(grandpa, &names). Then print the names separated by spaces using fmt.Println and strings.Join.
DSA Go
Hint
Create an empty slice names. Call postorder(grandpa, &names). Use fmt.Println(strings.Join(names, " ")) to print the names separated by spaces.
Practice
(1/5)
1. What is the order of nodes visited in a postorder tree traversal?
easy
A. Root node, Right subtree, Left subtree
B. Root node, Left subtree, Right subtree
C. Left subtree, Right subtree, Root node
D. Right subtree, Left subtree, Root node
Solution
Step 1: Understand postorder traversal definition
Postorder traversal visits the left subtree first, then the right subtree, and finally the root node.
Step 2: Match the order with options
Left subtree, Right subtree, Root node matches the order Left, Right, Root exactly.
Final Answer:
Left subtree, Right subtree, Root node -> Option C
Quick Check:
Postorder = Left, Right, Root [OK]
Hint: Remember postorder ends with root after children [OK]
Common Mistakes:
Confusing postorder with preorder or inorder
Visiting root before children
Swapping left and right subtree order
2. Which of the following Go function signatures correctly represents a recursive postorder traversal of a binary tree node root?
easy
A. func postorder(root *Node) { postorder(root.Left); postorder(root.Right); fmt.Println(root.Value) }
B. func postorder(root *Node) { fmt.Println(root.Value); postorder(root.Left); postorder(root.Right) }
C. func postorder(root *Node) { postorder(root.Right); postorder(root.Left); fmt.Println(root.Value) }
D. func postorder(root *Node) { fmt.Println(root.Value); postorder(root.Right); postorder(root.Left) }
Solution
Step 1: Recall postorder traversal steps
Postorder visits left subtree, then right subtree, then root node.
Step 2: Check function calls and print order
func postorder(root *Node) { postorder(root.Left); postorder(root.Right); fmt.Println(root.Value) } calls postorder on left, then right, then prints root value, matching postorder.
A. The order of recursive calls is swapped; left should be before right
B. The base case is missing for nil nodes
C. The root value should be printed before recursive calls
D. The function should return a slice instead of printing
Solution
Step 1: Analyze recursive call order
The code calls postorder on right subtree first, then left subtree, which reverses the expected order.
Step 2: Recall correct postorder sequence
Postorder requires left subtree first, then right subtree, then root. Swapping left and right causes wrong output.
Final Answer:
The order of recursive calls is swapped; left should be before right -> Option A
Quick Check:
Left before Right in postorder [OK]
Hint: Left subtree call must come before right subtree call [OK]
Common Mistakes:
Swapping left and right subtree calls
Printing root before children
Ignoring base case for nil
5. You want to collect the values of a binary tree in postorder traversal into a slice instead of printing. Which Go function correctly implements this?
hard
A. func postorder(root *Node, res *[]int) {
if root == nil {
return
}
postorder(root.Right, res)
postorder(root.Left, res)
*res = append(*res, root.Value)
}
B. func postorder(root *Node) []int {
if root == nil {
return []int{}
}
left := postorder(root.Left)
right := postorder(root.Right)
return append(left, append(right, root.Value)...)
}
C. func postorder(root *Node, res *[]int) {
if root == nil {
return
}
*res = append(*res, root.Value)
postorder(root.Left, res)
postorder(root.Right, res)
}
D. func postorder(root *Node) []int {
if root == nil {
return nil
}
return []int{root.Value}
}
Solution
Step 1: Understand collecting values in slice recursively
We want a function returning a slice with postorder values: left subtree slice + right subtree slice + root value.
Step 2: Check each option for correct order and slice handling
func postorder(root *Node) []int {
if root == nil {
return []int{}
}
left := postorder(root.Left)
right := postorder(root.Right)
return append(left, append(right, root.Value)...)
} correctly returns left slice, then appends right slice, then root value, matching postorder traversal.
Final Answer:
func postorder(root *Node) []int {
if root == nil {
return []int{}
}
left := postorder(root.Left)
right := postorder(root.Right)
return append(left, append(right, root.Value)...)
} -> Option B
Quick Check:
Return slices: left + right + root [OK]
Hint: Return left + right + root slices combined [OK]
Common Mistakes:
Appending root before children slices
Not combining slices correctly
Returning nil instead of empty slice for nil node
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.