Bird
Raised Fist0
DSA Pythonprogramming

Left Side View of Binary Tree in DSA Python

Choose your learning style10 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 it to the left side view
Level 0: [1]
Left side view: 1 -> null
Why: Root is always visible from the left side
Step 2: Move to level 1, nodes are 2 and 3; add leftmost node 2 to view
Level 1: [2 -> 3]
Left side view: 1 -> 2 -> null
Why: At level 1, node 2 is the first visible from the left
Step 3: Move to level 2, nodes are 4, 5, and 6; add leftmost node 4 to view
Level 2: [4 -> 5 -> 6]
Left side view: 1 -> 2 -> 4 -> null
Why: At level 2, node 4 is the first visible from the left
Result:
1 -> 2 -> 4 -> null
Annotated Code
DSA Python
class TreeNode:
    def __init__(self, val=0, left=None, right=None):
        self.val = val
        self.left = left
        self.right = right

def left_side_view(root):
    if not root:
        return []
    from collections import deque
    queue = deque([root])
    view = []
    while queue:
        level_length = len(queue)
        for i in range(level_length):
            node = queue.popleft()  # dequeue node
            if i == 0:
                view.append(node.val)  # first node at this level
            if node.left:
                queue.append(node.left)  # enqueue left child
            if node.right:
                queue.append(node.right)  # enqueue right child
    return view

# Driver code to build tree and print left side view
root = TreeNode(1)
root.left = TreeNode(2)
root.right = TreeNode(3)
root.left.left = TreeNode(4)
root.left.right = TreeNode(5)
root.right.right = TreeNode(6)

result = left_side_view(root)
print(' -> '.join(map(str, result)) + ' -> null')
node = queue.popleft() # dequeue node
remove the current node from the queue to process it
if i == 0: view.append(node.val) # first node at this level
add the first node of each level to the left side view
if node.left: queue.append(node.left) # enqueue left child
add left child to queue to process next level
if node.right: queue.append(node.right) # enqueue right child
add right child to queue to process next level
OutputSuccess
1 -> 2 -> 4 -> null
Complexity Analysis
Time: O(n) because we visit each node exactly once during the breadth-first traversal
Space: O(n) because in the worst case the queue holds all nodes at the largest level
vs Alternative: Compared to a depth-first approach, this breadth-first method 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 None)
Returns an empty list since there are no nodes to view
DSA Python
if not root:
    return []
Tree with only root node
Returns list with only root value as the left side view
DSA Python
if i == 0:
    view.append(node.val)  # first node at this level
Tree where some nodes have only right children
Still correctly picks the first node at each level, even if left child is missing
DSA Python
if node.left:
    queue.append(node.left)
When to Use This Pattern
When asked to find the visible nodes from one side of a tree, use a level order traversal and pick the first node at each level to get the side view.
Common Mistakes
Mistake: Adding all nodes at each level instead of only the first node
Fix: Add only the first node at each level by checking if the node is the first in the current level loop
Mistake: Using depth-first traversal without tracking levels properly
Fix: Use breadth-first traversal (queue) to process nodes level by level for easier left side detection
Summary
Finds the first node visible at each level when looking at the tree from the left side.
Use it when you need to see the leftmost nodes of each level in a binary tree.
The key insight is to traverse level by level and pick the first node encountered at each level.

Practice

(1/5)
1. What does the Left Side View of a binary tree represent?
easy
A. Nodes visible when the tree is viewed from the right side
B. Nodes at the deepest level of the tree
C. All leaf nodes of the tree
D. Nodes visible when the tree is viewed from the left side

Solution

  1. Step 1: Understand the view perspective

    The left side view shows nodes visible when looking at the tree from the left side.
  2. Step 2: Identify visible nodes

    Only the first node at each level from the left is visible in this view.
  3. Final Answer:

    Nodes visible when the tree is viewed from the left side -> Option D
  4. Quick Check:

    Left side view = nodes visible from left [OK]
Hint: Left side view shows first node at each level from left [OK]
Common Mistakes:
  • Confusing left side view with right side view
  • Thinking it shows all leaf nodes
  • Assuming it shows deepest nodes only
2. Which of the following Python code snippets correctly initializes an empty list to store the left side view nodes?
easy
A. left_view = ()
B. left_view = {}
C. left_view = []
D. left_view = set()

Solution

  1. Step 1: Identify the correct data structure

    We need a list to store nodes in order, so an empty list is best.
  2. Step 2: Check syntax for list initialization

    Using square brackets [] creates an empty list in Python.
  3. Final Answer:

    left_view = [] -> Option C
  4. Quick Check:

    Empty list = [] [OK]
Hint: Use [] to create empty list for ordered nodes [OK]
Common Mistakes:
  • Using {} which creates a dictionary, not a list
  • Using () which creates a tuple, not a list
  • Using set() which is unordered
3. What is the output of the following code that prints the left side view of the given binary tree?
class Node:
    def __init__(self, val):
        self.val = val
        self.left = None
        self.right = None

def left_side_view(root):
    result = []
    def dfs(node, level):
        if not node:
            return
        if level == len(result):
            result.append(node.val)
        dfs(node.left, level + 1)
        dfs(node.right, level + 1)
    dfs(root, 0)
    return result

root = Node(1)
root.left = Node(2)
root.right = Node(3)
root.left.right = Node(4)
root.right.right = Node(5)

print(left_side_view(root))
medium
A. [1, 2, 3]
B. [1, 2, 4]
C. [1, 3, 5]
D. [1, 4, 5]

Solution

  1. Step 1: Trace DFS traversal and level checks

    At level 0, node 1 is added. At level 1, node 2 is added (left child). At level 2, node 4 is added (left child's right child).
  2. Step 2: Confirm nodes added only once per level

    Right children at each level are ignored if left child exists at that level.
  3. Final Answer:

    [1, 2, 4] -> Option B
  4. Quick Check:

    Left side view nodes = [1, 2, 4] [OK]
Hint: Add node if level equals result length during DFS [OK]
Common Mistakes:
  • Adding right child nodes before left child nodes
  • Appending nodes multiple times per level
  • Confusing left and right children order
4. Find the error in the following code that attempts to print the left side view of a binary tree:
def left_side_view(root):
    result = []
    def dfs(node, level):
        if node is None:
            return
        if level < len(result):
            result.append(node.val)
        dfs(node.left, level + 1)
        dfs(node.right, level + 1)
    dfs(root, 0)
    return result
medium
A. The condition should be 'level == len(result)', not 'level < len(result)'
B. The base case should check 'if node' instead of 'if node is None'
C. The recursive calls should swap left and right children
D. The function should return 'result' inside the dfs function

Solution

  1. Step 1: Analyze the condition for adding nodes

    The code uses 'level < len(result)' which is incorrect because nodes should be added only when level equals the current result length.
  2. Step 2: Correct the condition to add nodes once per level

    Changing condition to 'level == len(result)' ensures only the first node at each level is added.
  3. Final Answer:

    The condition should be 'level == len(result)', not 'level < len(result)' -> Option A
  4. Quick Check:

    Use '==' to add first node per level [OK]
Hint: Add node only when level equals result length [OK]
Common Mistakes:
  • Using '<' instead of '==' in condition
  • Returning result inside recursive function
  • Swapping left and right child calls unnecessarily
5. Given a binary tree, how would you modify the standard left side view algorithm to return the left side view nodes in reverse order (bottom to top)?
hard
A. Perform the usual left side view traversal, then reverse the result list before returning
B. Traverse the tree from right to left instead of left to right
C. Add nodes to the result list only if they are leaf nodes
D. Use a queue and perform level order traversal from right to left

Solution

  1. Step 1: Perform standard left side view traversal

    Use DFS or BFS to collect the first node at each level from left to right.
  2. Step 2: Reverse the collected list

    After traversal, reverse the list to get nodes from bottom to top.
  3. Final Answer:

    Perform the usual left side view traversal, then reverse the result list before returning -> Option A
  4. Quick Check:

    Reverse after left side view traversal [OK]
Hint: Reverse the left side view list after traversal [OK]
Common Mistakes:
  • Changing traversal order instead of reversing result
  • Collecting leaf nodes only
  • Using right to left traversal which gives wrong nodes
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard