0
0
DSA Goprogramming~10 mins

Zigzag Level Order Traversal in DSA Go - Interactive Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to initialize the queue with the root node.

DSA Go
queue := []*TreeNode[1]
Drag options to blanks, or click blank then click option'
Aroot
Bnil
C[]
DTreeNode{}
Attempts:
3 left
💡 Hint
Common Mistakes
Starting with an empty queue
Using nil instead of root
2fill in blank
medium

Complete the code to toggle the direction for zigzag traversal.

DSA Go
leftToRight = ![1]
Drag options to blanks, or click blank then click option'
Adirection
BrightToLeft
CleftToRight
Dflag
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong variable name
Not toggling the direction
3fill in blank
hard

Fix the error in the code to append child nodes to the queue correctly.

DSA Go
if node.Left != nil {
    queue = append(queue, [1])
}
if node.Right != nil {
    queue = append(queue, node.Right)
}
Drag options to blanks, or click blank then click option'
Anode.Left
Bnode.Right
Cnil
Dnode
Attempts:
3 left
💡 Hint
Common Mistakes
Appending wrong child
Appending nil
4fill in blank
hard

Fill both blanks to correctly reverse the level slice when needed.

DSA Go
if !leftToRight {
    for i, j := 0, len(level)-1; i < j; i, j = i+1, [1] {
        level[i], level[j] = level[j], level[i]
    }
}

queue = queue[[2]:]
Drag options to blanks, or click blank then click option'
Aj-1
Blen(level)
D1
Attempts:
3 left
💡 Hint
Common Mistakes
Incorrect loop decrement
Wrong queue slicing
5fill in blank
hard

Fill all three blanks to build the zigzag level order traversal correctly.

DSA Go
result := [][]int{}
queue := []*TreeNode[1]
leftToRight := true

for len(queue) > 0 {
    levelSize := len(queue)
    level := make([]int, levelSize)
    for i := 0; i < levelSize; i++ {
        node := queue[0]
        queue = queue[[2]:]
        if leftToRight {
            level[i] = node.Val
        } else {
            level[levelSize-1-[3]] = node.Val
        }
        if node.Left != nil {
            queue = append(queue, node.Left)
        }
        if node.Right != nil {
            queue = append(queue, node.Right)
        }
    }
    result = append(result, level)
    leftToRight = !leftToRight
}
Drag options to blanks, or click blank then click option'
Aroot
B1
Ci
D0
Attempts:
3 left
💡 Hint
Common Mistakes
Wrong queue initialization
Incorrect index usage
Not removing nodes from queue