0
0
DSA Goprogramming~20 mins

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

Choose your learning style9 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.