Bird
Raised Fist0
DSA Goprogramming

Tree Traversal Postorder Left Right Root in DSA Go

Choose your learning style10 modes available
Mental Model
Visit the left child, then the right child, and finally the node itself. This order ensures children are processed before their parent.
Analogy: Imagine cleaning a tree house: first clean the left room, then the right room, and only after both rooms are clean, clean the main hall.
    1
   / \
  2   3
 / \
4   5

↑ root at 1
Dry Run Walkthrough
Input: Tree: 1 with left child 2 and right child 3; 2 has children 4 (left) and 5 (right)
Goal: Print nodes in postorder: left subtree, right subtree, then root
Step 1: Visit left subtree of 1: move to node 2
1 -> [curr->2] -> 3
2 -> 4 -> 5
Why: We must process left child before root
Step 2: Visit left subtree of 2: move to node 4
2 -> [curr->4] -> 5
Why: Continue left before right or root
Step 3: Node 4 has no children, print 4
Printed: 4
Why: Leaf node reached, print now
Step 4: Visit right subtree of 2: move to node 5
2 -> 4 -> [curr->5]
Why: After left subtree, process right subtree
Step 5: Node 5 has no children, print 5
Printed: 4 5
Why: Leaf node reached, print now
Step 6: Print node 2 after its children
Printed: 4 5 2
Why: Children processed, now print parent
Step 7: Visit right subtree of 1: move to node 3
1 -> 2 -> [curr->3]
Why: After left subtree, process right subtree
Step 8: Node 3 has no children, print 3
Printed: 4 5 2 3
Why: Leaf node reached, print now
Step 9: Print root node 1 after its children
Printed: 4 5 2 3 1
Why: All children processed, print root last
Result:
4 5 2 3 1
Annotated Code
DSA Go
package main

import "fmt"

type TreeNode struct {
    Val   int
    Left  *TreeNode
    Right *TreeNode
}

func postorderTraversal(root *TreeNode) {
    if root == nil {
        return
    }
    postorderTraversal(root.Left) // traverse left subtree first
    postorderTraversal(root.Right) // then traverse right subtree
    fmt.Printf("%d ", root.Val) // print root after children
}

func main() {
    // Construct the tree:
    //     1
    //    / \
    //   2   3
    //  / \
    // 4   5
    root := &TreeNode{Val: 1}
    root.Left = &TreeNode{Val: 2}
    root.Right = &TreeNode{Val: 3}
    root.Left.Left = &TreeNode{Val: 4}
    root.Left.Right = &TreeNode{Val: 5}

    postorderTraversal(root)
    fmt.Println()
}
postorderTraversal(root.Left) // traverse left subtree first
advance to left child to process left subtree
postorderTraversal(root.Right) // then traverse right subtree
after left subtree, process right subtree
fmt.Printf("%d ", root.Val) // print root after children
print current node after children processed
OutputSuccess
4 5 2 3 1
Complexity Analysis
Time: O(n) because each node is visited exactly once
Space: O(h) where h is tree height due to recursion stack
vs Alternative: Compared to iterative traversal, recursive is simpler but uses call stack; iterative may use explicit stack with similar time
Edge Cases
Empty tree (root is nil)
Function returns immediately without printing
DSA Go
if root == nil {
        return
    }
Single node tree
Prints the single node value
DSA Go
if root == nil {
        return
    }
When to Use This Pattern
When you need to process children before their parent in a tree, use postorder traversal to ensure left and right subtrees are handled first.
Common Mistakes
Mistake: Printing the root before children (preorder) instead of after
Fix: Move the print statement after recursive calls to left and right children
Summary
Visits left subtree, then right subtree, then the node itself.
Use when you need to process children before their parent, like deleting a tree.
The key is to print the node only after both children are processed.

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

  1. Step 1: Understand postorder traversal definition

    Postorder traversal visits the left subtree first, then the right subtree, and finally the root node.
  2. Step 2: Match the order with options

    Left subtree, Right subtree, Root node matches the order Left, Right, Root exactly.
  3. Final Answer:

    Left subtree, Right subtree, Root node -> Option C
  4. 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

  1. Step 1: Recall postorder traversal steps

    Postorder visits left subtree, then right subtree, then root node.
  2. 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.
  3. Final Answer:

    func postorder(root *Node) { postorder(root.Left); postorder(root.Right); fmt.Println(root.Value) } -> Option A
  4. Quick Check:

    Calls: Left, Right, Print root [OK]
Hint: Print root after recursive calls to left and right [OK]
Common Mistakes:
  • Printing root before recursive calls
  • Swapping left and right recursive calls
  • Missing recursive calls for subtrees
3. Given the binary tree below, what is the output of the postorder traversal?

      1
     / \
    2   3
   / \
  4   5
func postorder(root *Node) { if root == nil { return } postorder(root.Left) postorder(root.Right) fmt.Print(root.Value, " ") }
medium
A. 4 2 5 3 1
B. 1 2 4 5 3
C. 2 4 5 3 1
D. 4 5 2 3 1

Solution

  1. Step 1: Traverse left subtree of root (1)

    Left subtree is rooted at 2. Postorder of subtree 2 is: left(4), right(5), root(2) -> 4 5 2.
  2. Step 2: Traverse right subtree and root

    Right subtree is 3 (leaf), so output 3. Then print root 1 last.
  3. Final Answer:

    4 5 2 3 1 -> Option D
  4. Quick Check:

    Postorder = Left, Right, Root = 4 5 2 3 1 [OK]
Hint: Visit left subtree fully, then right, then root [OK]
Common Mistakes:
  • Printing root before children
  • Mixing left and right subtree order
  • Skipping leaf nodes
4. The following Go code attempts a postorder traversal but produces incorrect output. What is the error?

func postorder(root *Node) {
  if root == nil {
    return
  }
  postorder(root.Right)
  postorder(root.Left)
  fmt.Print(root.Value, " ")
}
medium
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

  1. Step 1: Analyze recursive call order

    The code calls postorder on right subtree first, then left subtree, which reverses the expected order.
  2. Step 2: Recall correct postorder sequence

    Postorder requires left subtree first, then right subtree, then root. Swapping left and right causes wrong output.
  3. Final Answer:

    The order of recursive calls is swapped; left should be before right -> Option A
  4. 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

  1. 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.
  2. 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.
  3. 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
  4. 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 unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard