0
0
DSA Goprogramming~10 mins

Left Side View 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 initialize the queue with the root node.

DSA Go
queue := []*TreeNode[1]
Drag options to blanks, or click blank then click option'
Aroot
Bnil
Cnew(TreeNode)
DTreeNode{}
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.
2fill in blank
medium

Complete 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'
Alen(queue) - 1
B1
C0
Di
Attempts:
3 left
💡 Hint
Common Mistakes
Using index i which changes inside the loop.
Using last index instead of first.
3fill in blank
hard

Fix 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'
Alen(queue)
BlevelSize
Ccap(queue)
Dlen(result)
Attempts:
3 left
💡 Hint
Common Mistakes
Using len(queue) which changes during the loop.
Using cap(queue) which is capacity, not length.
4fill in blank
hard

Fill 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'
ALeft
BRight
Cleft
Dright
Attempts:
3 left
💡 Hint
Common Mistakes
Using uppercase Left or Right which do not exist.
Mixing left and right fields incorrectly.
5fill in blank
hard

Fill 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'
A[]int{}
B0
ClevelSize
Dnil
Attempts:
3 left
💡 Hint
Common Mistakes
Returning nil instead of empty slice.
Appending wrong index from queue.
Looping wrong number of times.