Bird
Raised Fist0
DSA Goprogramming~20 mins

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

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Inorder Traversal Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
What is the output of inorder traversal on this binary tree?

Given the binary tree below, what is the inorder traversal output?

Tree structure:

    2
   / \
  1   3
DSA Go
package main
import "fmt"
type Node struct {
    Val   int
    Left  *Node
    Right *Node
}

func inorder(root *Node) {
    if root == nil {
        return
    }
    inorder(root.Left)
    fmt.Print(root.Val, " ")
    inorder(root.Right)
}

func main() {
    root := &Node{Val: 2}
    root.Left = &Node{Val: 1}
    root.Right = &Node{Val: 3}
    inorder(root)
}
A1 2 3
B2 1 3
C3 2 1
D1 3 2
Attempts:
2 left
💡 Hint

Inorder traversal visits left child, then root, then right child.

❓ Predict Output
intermediate
2:00remaining
What is the inorder traversal output of this tree?

Consider the binary tree:

      4
     / \
    2   5
   / \
  1   3

What is the output of inorder traversal?

DSA Go
package main
import "fmt"
type Node struct {
    Val   int
    Left  *Node
    Right *Node
}

func inorder(root *Node) {
    if root == nil {
        return
    }
    inorder(root.Left)
    fmt.Print(root.Val, " ")
    inorder(root.Right)
}

func main() {
    root := &Node{Val: 4}
    root.Left = &Node{Val: 2}
    root.Right = &Node{Val: 5}
    root.Left.Left = &Node{Val: 1}
    root.Left.Right = &Node{Val: 3}
    inorder(root)
}
A4 2 1 3 5
B1 2 3 4 5
C2 1 3 4 5
D1 3 2 4 5
Attempts:
2 left
💡 Hint

Remember inorder is left, root, right recursively.

🔧 Debug
advanced
2:00remaining
What error does this inorder traversal code produce?

Analyze the following Go code for inorder traversal. What error will it produce when run?

DSA Go
package main
import "fmt"
type Node struct {
    Val   int
    Left  *Node
    Right *Node
}

func inorder(root *Node) {
    if root == nil {
        return
    }
    inorder(root.Left)
    fmt.Print(root.Val)
    inorder(root.Right)
}

func main() {
    var root *Node
    inorder(root.Left)
}
Aruntime error: invalid memory address or nil pointer dereference
Bcompilation error: undefined variable root
Cno output, program runs successfully
Dcompilation error: missing return statement
Attempts:
2 left
💡 Hint

Check what happens when you access a field of a nil pointer.

🧠 Conceptual
advanced
2:00remaining
How many nodes are visited in inorder traversal of this tree?

Given the binary tree:

      10
     /  \
    5    15
   /    /  \
  3    12   20

How many nodes will inorder traversal visit?

A5
B7
C4
D6
Attempts:
2 left
💡 Hint

Count all nodes in the tree.

🚀 Application
expert
3:00remaining
What is the inorder traversal output after inserting 7 into this BST?

Given the BST:

      5
     / \
    3   8
   /   / \
  2   6   9

After inserting 7, what is the inorder traversal output?

DSA Go
package main
import "fmt"
type Node struct {
    Val   int
    Left  *Node
    Right *Node
}

func insert(root *Node, val int) *Node {
    if root == nil {
        return &Node{Val: val}
    }
    if val < root.Val {
        root.Left = insert(root.Left, val)
    } else {
        root.Right = insert(root.Right, val)
    }
    return root
}

func inorder(root *Node) {
    if root == nil {
        return
    }
    inorder(root.Left)
    fmt.Print(root.Val, " ")
    inorder(root.Right)
}

func main() {
    root := &Node{Val: 5}
    root.Left = &Node{Val: 3}
    root.Right = &Node{Val: 8}
    root.Left.Left = &Node{Val: 2}
    root.Right.Left = &Node{Val: 6}
    root.Right.Right = &Node{Val: 9}
    root = insert(root, 7)
    inorder(root)
}
A2 3 5 7 6 8 9
B2 3 5 6 8 7 9
C2 3 5 6 7 8 9
D2 3 5 6 8 9 7
Attempts:
2 left
💡 Hint

Inorder traversal of BST prints nodes in ascending order.

Practice

(1/5)
1. What is the order of nodes visited in an inorder traversal of a binary tree?
easy
A. Root node, Right subtree, Left subtree
B. Root node, Left subtree, Right subtree
C. Right subtree, Root node, Left subtree
D. Left subtree, Root node, Right subtree

Solution

  1. Step 1: Understand inorder traversal definition

    Inorder traversal means visiting the left subtree first, then the root node, and finally the right subtree.
  2. Step 2: Apply the traversal order

    Following this order ensures nodes are visited in ascending order for binary search trees.
  3. Final Answer:

    Left subtree, Root node, Right subtree -> Option D
  4. Quick Check:

    Inorder = Left, Root, Right [OK]
Hint: Remember: Inorder = Left, Root, Right [OK]
Common Mistakes:
  • Confusing inorder with preorder or postorder
  • Visiting root before left subtree
  • Visiting right subtree before root
2. Which of the following Go function signatures correctly represents an inorder traversal function for a binary tree node defined as type Node struct { Val int; Left, Right *Node }?
easy
A. func inorder(root *Node) []int
B. func inorder(root Node) []int
C. func inorder(root *Node) int
D. func inorder(root Node) int

Solution

  1. Step 1: Understand pointer usage in Go for trees

    Binary trees use pointers to nodes to allow recursive traversal and modification.
  2. Step 2: Choose function signature returning slice of ints

    Inorder traversal returns a list of node values, so return type should be []int, and input should be pointer to Node.
  3. Final Answer:

    func inorder(root *Node) []int -> Option A
  4. Quick Check:

    Pointer receiver and slice return type [OK]
Hint: Use pointer receiver and return slice for traversal [OK]
Common Mistakes:
  • Passing Node by value instead of pointer
  • Returning single int instead of slice
  • Using incorrect return type
3. What is the output of the inorder traversal for the following binary tree?

    2
   / \
  1   3
Given the Go code:
type Node struct {
    Val int
    Left, Right *Node
}

func inorder(root *Node) []int {
    if root == nil {
        return []int{}
    }
    left := inorder(root.Left)
    right := inorder(root.Right)
    return append(append(left, root.Val), right...)
}

func main() {
    root := &Node{2, &Node{1, nil, nil}, &Node{3, nil, nil}}
    result := inorder(root)
    fmt.Println(result)
}
medium
A. [3, 2, 1]
B. [2, 1, 3]
C. [1, 2, 3]
D. [1, 3, 2]

Solution

  1. Step 1: Trace inorder traversal on given tree

    Visit left subtree (1), then root (2), then right subtree (3).
  2. Step 2: Confirm output list order

    The traversal returns [1, 2, 3] as expected for inorder.
  3. Final Answer:

    [1, 2, 3] -> Option C
  4. Quick Check:

    Inorder traversal output = [1, 2, 3] [OK]
Hint: Inorder visits left, root, then right [OK]
Common Mistakes:
  • Appending root before left subtree
  • Appending right subtree before root
  • Confusing preorder or postorder output
4. The following Go code attempts inorder traversal but produces incorrect output. What is the bug?
func inorder(root *Node) []int {
    if root == nil {
        return []int{}
    }
    left := inorder(root.Left)
    right := inorder(root.Right)
    return append(left, right...)
    return append(result, root.Val)
}
medium
A. The root value is appended after the return statement, so never executed
B. The function should return int, not []int
C. The base case should return nil, not empty slice
D. The left subtree is not traversed

Solution

  1. Step 1: Analyze return statements in function

    The first return statement returns append(left, right...), so the second return is unreachable.
  2. Step 2: Identify missing root value append

    Root value is never appended because code after first return is ignored.
  3. Final Answer:

    The root value is appended after the return statement, so never executed -> Option A
  4. Quick Check:

    Code after return is unreachable [OK]
Hint: Check for unreachable code after return [OK]
Common Mistakes:
  • Ignoring unreachable code after return
  • Not appending root value in correct order
  • Confusing base case return value
5. Given a binary tree where some nodes have only one child, how does inorder traversal help in printing the nodes in sorted order if the tree is a binary search tree? Consider the tree:

      5
     / \
    3   8
     \   \
      4   10
What is the inorder traversal output?
hard
A. [5, 3, 4, 8, 10]
B. [3, 4, 5, 8, 10]
C. [4, 3, 5, 10, 8]
D. [3, 5, 4, 8, 10]

Solution

  1. Step 1: Apply inorder traversal on given tree

    Visit left subtree: node 3, then its right child 4, then root 5, then right subtree nodes 8 and 10.
  2. Step 2: List nodes in inorder sequence

    Sequence is 3, 4, 5, 8, 10 which is sorted ascending order.
  3. Final Answer:

    [3, 4, 5, 8, 10] -> Option B
  4. Quick Check:

    Inorder traversal of BST gives sorted nodes [OK]
Hint: Inorder traversal of BST prints nodes sorted [OK]
Common Mistakes:
  • Ignoring nodes with single child
  • Visiting root before left subtree
  • Mixing preorder or postorder with inorder
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