0
0
DSA Goprogramming~10 mins

Maximum Width of Binary Tree 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 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'
ATreeNode
B*TreeNode
Cint
Dbool
Attempts:
3 left
💡 Hint
Common Mistakes
Using TreeNode instead of *TreeNode for child nodes.
Using int or bool types for child nodes.
2fill in blank
medium

Complete the code to initialize a queue for BFS traversal.

DSA Go
queue := []*TreeNode[1]
Drag options to blanks, or click blank then click option'
A{}
B[]
Cnil
D0
Attempts:
3 left
💡 Hint
Common Mistakes
Using {} which is invalid syntax for slices.
Using 0 which is an int, not a slice.
3fill in blank
hard

Fix 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'
A=
B==
C+=
D-=
Attempts:
3 left
💡 Hint
Common Mistakes
Using == instead of = causes no update.
Using += or -= changes value incorrectly.
4fill in blank
hard

Fill 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'
AIndex
BVal
CDepth
DHeight
Attempts:
3 left
💡 Hint
Common Mistakes
Using Val instead of Index causes wrong width calculation.
Using Depth or Height fields which are not defined.
5fill in blank
hard

Fill 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'
AIndex
BVal
CLeft
DRight
Attempts:
3 left
💡 Hint
Common Mistakes
Using Val or child pointers instead of Index for position.
Mixing Left and Right fields incorrectly.