0
0
DSA Goprogramming~20 mins

Tree Traversal Postorder Left Right Root in DSA Go - Practice Problems & Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Postorder Traversal Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of Postorder Traversal on a Simple Binary Tree
What is the output of the postorder traversal (Left, Right, Root) for the given binary tree?
DSA Go
package main

import "fmt"

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

func postorder(root *Node, result *[]int) {
    if root == nil {
        return
    }
    postorder(root.Left, result)
    postorder(root.Right, result)
    *result = append(*result, root.Val)
}

func main() {
    root := &Node{Val: 1}
    root.Left = &Node{Val: 2}
    root.Right = &Node{Val: 3}
    root.Left.Left = &Node{Val: 4}
    root.Left.Right = &Node{Val: 5}

    var result []int
    postorder(root, &result)
    fmt.Println(result)
}
A[2, 4, 5, 3, 1]
B[1, 2, 3, 4, 5]
C[4, 2, 5, 3, 1]
D[4, 5, 2, 3, 1]
Attempts:
2 left
💡 Hint
Remember postorder means visit left subtree, then right subtree, then the root node.
🧠 Conceptual
intermediate
1:00remaining
Understanding Postorder Traversal Steps
Which of the following best describes the order of visiting nodes in postorder traversal?
ALeft, Right, Root
BRoot, Left, Right
CRight, Left, Root
DLeft, Root, Right
Attempts:
2 left
💡 Hint
Postorder traversal always visits the root last.
🔧 Debug
advanced
2:00remaining
Identify the Bug in Postorder Traversal Implementation
What error will this code produce when performing postorder traversal?
DSA Go
package main

import "fmt"

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

func postorder(root *Node, result *[]int) {
    if root == nil {
        return
    }
    postorder(root.Right, result)
    postorder(root.Left, result)
    *result = append(*result, root.Val)
}

func main() {
    root := &Node{Val: 1}
    root.Left = &Node{Val: 2}
    root.Right = &Node{Val: 3}

    var result []int
    postorder(root, &result)
    fmt.Println(result)
}
AOutput: [3, 2, 1] but order is incorrect for postorder
BOutput: [3, 2, 1]
COutput: [2, 3, 1]
DRuntime error: nil pointer dereference
Attempts:
2 left
💡 Hint
Check the order of recursive calls for left and right children.
🚀 Application
advanced
2:00remaining
Postorder Traversal Output for Unbalanced Tree
Given the following unbalanced binary tree, what is the postorder traversal output?
DSA Go
package main

import "fmt"

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

func postorder(root *Node, result *[]int) {
    if root == nil {
        return
    }
    postorder(root.Left, result)
    postorder(root.Right, result)
    *result = append(*result, root.Val)
}

func main() {
    root := &Node{Val: 10}
    root.Right = &Node{Val: 20}
    root.Right.Left = &Node{Val: 15}
    root.Right.Right = &Node{Val: 25}

    var result []int
    postorder(root, &result)
    fmt.Println(result)
}
A[15, 20, 25, 10]
B[10, 15, 25, 20]
C[15, 25, 20, 10]
D[20, 15, 25, 10]
Attempts:
2 left
💡 Hint
Remember to visit left subtree, then right subtree, then root.
🧠 Conceptual
expert
0:30remaining
Number of Nodes Visited in Postorder Traversal
If a binary tree has 7 nodes, how many nodes will be visited during a complete postorder traversal?
A6
B7
C8
DDepends on the tree structure
Attempts:
1 left
💡 Hint
Postorder traversal visits every node exactly once.