Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Initializing queue with nil or empty node instead of root.
Using new(TreeNode) which creates an empty node, not the root.
✗ Incorrect
We start the level order traversal by adding the root node to the queue.
2fill in blank
mediumComplete the code to append the first node's value of the current level to the result.
DSA Go
result = append(result, queue[[1]].Val) Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using index i which changes inside the loop.
Using last index instead of first.
✗ Incorrect
The first node in the queue at the start of each level is at index 0.
3fill in blank
hardFix the error in the loop condition to process all nodes at the current level.
DSA Go
for i := 0; i < [1]; i++ {
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using len(queue) which changes during the loop.
Using cap(queue) which is capacity, not length.
✗ Incorrect
We process exactly levelSize nodes at each level to avoid mixing levels.
4fill in blank
hardFill both blanks to add left and right children to the queue if they exist.
DSA Go
if node.[1] != nil { queue = append(queue, node.[2]) }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using uppercase Left or Right which do not exist.
Mixing left and right fields incorrectly.
✗ Incorrect
In Go, struct fields are case-sensitive; left and right are lowercase.
5fill in blank
hardFill all three blanks to complete the function returning the left side view of the binary tree.
DSA Go
func leftSideView(root *TreeNode) []int {
if root == nil {
return [1]
}
result := []int{}
queue := []*TreeNode{root}
for len(queue) > 0 {
levelSize := len(queue)
result = append(result, queue[[2]].Val)
for i := 0; i < [3]; i++ {
node := queue[0]
queue = queue[1:]
if node.left != nil {
queue = append(queue, node.left)
}
if node.right != nil {
queue = append(queue, node.right)
}
}
}
return result
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Returning nil instead of empty slice.
Appending wrong index from queue.
Looping wrong number of times.
✗ Incorrect
Return empty slice if root is nil; append first node at index 0; loop levelSize times.