Challenge - 5 Problems
Level Order BFS Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of Level Order Traversal on a Simple Tree
What is the output of the following Go code performing level order traversal (BFS) on a binary tree?
DSA Go
package main import "fmt" type TreeNode struct { Val int Left *TreeNode Right *TreeNode } func levelOrder(root *TreeNode) []int { if root == nil { return []int{} } queue := []*TreeNode{root} result := []int{} for len(queue) > 0 { node := queue[0] queue = queue[1:] result = append(result, node.Val) if node.Left != nil { queue = append(queue, node.Left) } if node.Right != nil { queue = append(queue, node.Right) } } return result } func main() { root := &TreeNode{Val: 1} root.Left = &TreeNode{Val: 2} root.Right = &TreeNode{Val: 3} root.Left.Left = &TreeNode{Val: 4} root.Left.Right = &TreeNode{Val: 5} fmt.Println(levelOrder(root)) }
Attempts:
2 left
💡 Hint
Remember, level order traversal visits nodes level by level from left to right.
✗ Incorrect
The traversal visits root first (1), then its children (2, 3), then the next level children (4, 5). So the output is [1 2 3 4 5].
🧠 Conceptual
intermediate1:30remaining
Understanding Queue Usage in Level Order Traversal
Why is a queue used in level order traversal (BFS) of a binary tree instead of a stack?
Attempts:
2 left
💡 Hint
Think about the order nodes are visited in BFS.
✗ Incorrect
A queue processes nodes in the order they were added (first-in, first-out), which matches the level order traversal visiting nodes level by level. A stack uses last-in, first-out order, which is not suitable for BFS.
🔧 Debug
advanced2:00remaining
Identify the Bug in Level Order Traversal Code
What error will the following Go code produce when performing level order traversal?
DSA Go
func levelOrder(root *TreeNode) []int { var queue []*TreeNode var result []int queue = append(queue, root) for len(queue) > 0 { node := queue[0] queue = queue[1:] result = append(result, node.Val) if node.Left != nil { queue = append(queue, node.Left) } if node.Right != nil { queue = append(queue, node.Right) } } return result }
Attempts:
2 left
💡 Hint
What happens if root is nil?
✗ Incorrect
If root is nil, it is appended to the queue. Then node.Val causes a nil pointer dereference runtime error.
❓ Predict Output
advanced2:00remaining
Output of Level Order Traversal with Unbalanced Tree
What is the output of the following Go code performing level order traversal on an unbalanced binary tree?
DSA Go
package main import "fmt" type TreeNode struct { Val int Left *TreeNode Right *TreeNode } func levelOrder(root *TreeNode) []int { if root == nil { return []int{} } queue := []*TreeNode{root} result := []int{} for len(queue) > 0 { node := queue[0] queue = queue[1:] result = append(result, node.Val) if node.Left != nil { queue = append(queue, node.Left) } if node.Right != nil { queue = append(queue, node.Right) } } return result } func main() { root := &TreeNode{Val: 10} root.Right = &TreeNode{Val: 20} root.Right.Right = &TreeNode{Val: 30} fmt.Println(levelOrder(root)) }
Attempts:
2 left
💡 Hint
Level order visits nodes level by level, left to right.
✗ Incorrect
The tree has nodes 10 at root, 20 as right child, and 30 as right child of 20. The traversal visits 10, then 20, then 30.
🚀 Application
expert3:00remaining
Number of Nodes at Each Level Using Level Order Traversal
Given a binary tree, which code snippet correctly returns a slice of integers representing the count of nodes at each level using level order traversal in Go?
Attempts:
2 left
💡 Hint
Count nodes before processing each level, and process exactly that many nodes per level.
✗ Incorrect
Option A correctly counts nodes per level by recording the queue length before processing that level's nodes. Other options either count incorrectly or have off-by-one errors.