0
0
DSA Goprogramming~20 mins

Left Side View of Binary Tree in DSA Go - Practice Problems & Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Left Side View Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of Left Side View for a Simple Binary Tree
What is the output of the left side view function when run on the following binary tree?
DSA Go
type TreeNode struct {
    Val   int
    Left  *TreeNode
    Right *TreeNode
}

func leftSideView(root *TreeNode) []int {
    if root == nil {
        return []int{}
    }
    var result []int
    queue := []*TreeNode{root}
    for len(queue) > 0 {
        levelSize := len(queue)
        for i := 0; i < levelSize; i++ {
            node := queue[0]
            queue = queue[1:]
            if i == 0 {
                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
}

// Tree structure:
//       1
//      / \
//     2   3
//      \   \
//       5   4

root := &TreeNode{Val: 1}
root.Left = &TreeNode{Val: 2}
root.Right = &TreeNode{Val: 3}
root.Left.Right = &TreeNode{Val: 5}
root.Right.Right = &TreeNode{Val: 4}

output := leftSideView(root)
fmt.Println(output)
A[1 3 5]
B[1 3 4]
C[1 2 4]
D[1 2 5]
Attempts:
2 left
💡 Hint
Remember, the left side view shows the first node visible at each level from the left side.
🧠 Conceptual
intermediate
1:00remaining
Understanding Left Side View Node Selection
In the left side view of a binary tree, which nodes are included in the output?
AThe leftmost node at each level
BThe rightmost node at each level
CAll leaf nodes
DAll nodes with only one child
Attempts:
2 left
💡 Hint
Think about what you see when looking at the tree from the left side.
Predict Output
advanced
2:00remaining
Output of Left Side View for a Skewed Binary Tree
What is the output of the left side view function when run on this skewed binary tree?
DSA Go
type TreeNode struct {
    Val   int
    Left  *TreeNode
    Right *TreeNode
}

func leftSideView(root *TreeNode) []int {
    if root == nil {
        return []int{}
    }
    var result []int
    queue := []*TreeNode{root}
    for len(queue) > 0 {
        levelSize := len(queue)
        for i := 0; i < levelSize; i++ {
            node := queue[0]
            queue = queue[1:]
            if i == 0 {
                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
}

// Tree structure:
// 1
//  \
//   2
//    \
//     3
//      \
//       4

root := &TreeNode{Val: 1}
root.Right = &TreeNode{Val: 2}
root.Right.Right = &TreeNode{Val: 3}
root.Right.Right.Right = &TreeNode{Val: 4}

output := leftSideView(root)
fmt.Println(output)
A[4]
B[1]
C[1 2 3 4]
D[1 3]
Attempts:
2 left
💡 Hint
Even if nodes are only on the right, the left side view includes the first node at each level.
🔧 Debug
advanced
1:30remaining
Identify the Bug in Left Side View Implementation
What error will this code produce when trying to get the left side view of a binary tree?
DSA Go
func leftSideView(root *TreeNode) []int {
    if root == nil {
        return []int{}
    }
    var result []int
    queue := []*TreeNode{root}
    for len(queue) > 0 {
        levelSize := len(queue)
        for i := 0; i < levelSize; i++ {
            node := queue[0]
            queue = queue[1:]
            if i == 0 {
                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
}
ANo error, returns correct output
BIndex out of range runtime error
CCompilation error due to syntax
DInfinite loop
Attempts:
2 left
💡 Hint
Check the loop condition carefully for off-by-one errors.
🚀 Application
expert
1:30remaining
Number of Nodes in Left Side View of a Complete Binary Tree
Given a complete binary tree with height h (root at height 1), how many nodes will the left side view contain?
Ah
Bh - 1
C2^h - 1
D2^(h-1)
Attempts:
2 left
💡 Hint
The left side view shows one node per level.