0
0
DSA Goprogramming

Left Side View of Binary Tree in DSA Go

Choose your learning style9 modes available
Mental Model
We want to see the nodes visible when looking at the tree from the left side, which means the first node at each level.
Analogy: Imagine standing on the left side of a tall building with many floors; you only see the first window on each floor from your side.
      1
     / \
    2   3
   / \   \
  4   5   6

Left side view: 1 -> 2 -> 4 -> null
Dry Run Walkthrough
Input: Binary tree: 1 with left child 2 and right child 3; 2 has children 4 and 5; 3 has right child 6
Goal: Find the leftmost node at each level to form the left side view
Step 1: Start at root node 1, add 1 to left view
Level 0: [1]
Left view: 1
Why: Root is always visible from the left side
Step 2: Go to level 1, nodes are 2 and 3; add 2 to left view
Level 1: [2, 3]
Left view: 1 -> 2
Why: At level 1, the leftmost node is 2
Step 3: Go to level 2, nodes are 4, 5, and 6; add 4 to left view
Level 2: [4, 5, 6]
Left view: 1 -> 2 -> 4
Why: At level 2, the leftmost node is 4
Result:
1 -> 2 -> 4 -> null
Annotated Code
DSA Go
package main

import (
	"fmt"
)

type TreeNode struct {
	Val   int
	Left  *TreeNode
	Right *TreeNode
}

func leftSideView(root *TreeNode) []int {
	if root == nil {
		return []int{}
	}
	result := []int{}
	queue := []*TreeNode{root}

	for len(queue) > 0 {
		n := len(queue)
		for i := 0; i < n; i++ {
			curr := queue[0]
			queue = queue[1:]

			if i == 0 {
				result = append(result, curr.Val) // add first node of this level
			}

			if curr.Left != nil {
				queue = append(queue, curr.Left) // enqueue left child
			}
			if curr.Right != nil {
				queue = append(queue, curr.Right) // enqueue right child
			}
		}
	}
	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}
	root.Right.Right = &TreeNode{Val: 6}

	view := leftSideView(root)
	for _, v := range view {
		fmt.Printf("%d -> ", v)
	}
	fmt.Print("null\n")
}
if root == nil { return []int{} }
handle empty tree edge case
queue := []*TreeNode{root}
start BFS with root node in queue
for len(queue) > 0 { n := len(queue) }
process nodes level by level
curr := queue[0]; queue = queue[1:]
dequeue current node for processing
if i == 0 { result = append(result, curr.Val) }
add first node of each level to left view
if curr.Left != nil { queue = append(queue, curr.Left) }
enqueue left child for next level
if curr.Right != nil { queue = append(queue, curr.Right) }
enqueue right child for next level
OutputSuccess
1 -> 2 -> 4 -> null
Complexity Analysis
Time: O(n) because we visit each node once during the breadth-first search
Space: O(n) because the queue can hold up to all nodes at the widest level
vs Alternative: Compared to a recursive DFS approach, BFS naturally processes nodes level by level, making it easier to pick the first node at each level without extra bookkeeping
Edge Cases
Empty tree (root is nil)
Returns an empty list since there are no nodes to view
DSA Go
if root == nil { return []int{} }
Tree with only one node
Returns a list with just that node's value
DSA Go
queue := []*TreeNode{root}
Tree where some nodes have only right children
Still correctly picks the leftmost node at each level, which may be a right child if no left child exists
DSA Go
if curr.Left != nil { queue = append(queue, curr.Left) }
When to Use This Pattern
When asked to find the visible nodes from one side of a tree, especially the left or right side, use a level order traversal and pick the first node at each level.
Common Mistakes
Mistake: Adding all nodes at each level instead of only the first one
Fix: Add only the first node (i == 0) of each level to the result list
Mistake: Using preorder DFS without tracking levels properly
Fix: Use BFS or track levels carefully in DFS to ensure only the leftmost node per level is added
Summary
Finds the leftmost node at each level of a binary tree to show the left side view.
Use when you need to see which nodes are visible when looking at the tree from the left side.
The key insight is to traverse level by level and pick only the first node encountered at each level.