Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to declare a binary tree node struct.
DSA Go
type TreeNode struct {
Val int
Left *TreeNode
Right [1] *TreeNode
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using TreeNode instead of *TreeNode for child nodes.
Using int or bool types for child nodes.
✗ Incorrect
The Left and Right children of a binary tree node are pointers to TreeNode structs, so the type is *TreeNode.
2fill in blank
mediumComplete the code to initialize a queue for BFS traversal.
DSA Go
queue := []*TreeNode[1] Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using {} which is invalid syntax for slices.
Using 0 which is an int, not a slice.
✗ Incorrect
In Go, a nil slice can be used as an empty queue for BFS traversal.
3fill in blank
hardFix the error in the code to update the maximum width.
DSA Go
if width > maxWidth { maxWidth [1] width }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using == instead of = causes no update.
Using += or -= changes value incorrectly.
✗ Incorrect
To update maxWidth to the new maximum width, use the assignment operator =.
4fill in blank
hardFill both blanks to correctly calculate the width of the current level.
DSA Go
width := int(queue[len(queue)-1].[1] - queue[0].[2] + 1)
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using Val instead of Index causes wrong width calculation.
Using Depth or Height fields which are not defined.
✗ Incorrect
The width is calculated using the Index field of the first and last nodes in the queue.
5fill in blank
hardFill all three blanks to enqueue child nodes with correct indices.
DSA Go
if node.Left != nil { queue = append(queue, &TreeNode{Val: node.Left.Val, [1]: node.[2] * 2}) } if node.Right != nil { queue = append(queue, &TreeNode{Val: node.Right.Val, [3]: node.[2] * 2 + 1}) }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using Val or child pointers instead of Index for position.
Mixing Left and Right fields incorrectly.
✗ Incorrect
Child nodes get their Index field set based on parent's Index: left child is parent.Index * 2, right child is parent.Index * 2 + 1.