Left Side View of Binary Tree in DSA Python - Time & Space Complexity
We want to understand how the time needed to find the left side view of a binary tree changes as the tree grows.
Specifically, how does the number of steps grow when the tree has more nodes?
Analyze the time complexity of the following code snippet.
from collections import deque
def left_side_view(root):
if not root:
return []
result = []
queue = deque([root])
while queue:
level_length = len(queue)
for i in range(level_length):
node = queue.popleft()
if i == 0:
result.append(node.val)
if node.left:
queue.append(node.left)
if node.right:
queue.append(node.right)
return result
This code finds the left side view of a binary tree by visiting nodes level by level and recording the first node at each level.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Visiting each node once in a breadth-first manner using a queue.
- How many times: Each node is processed exactly one time.
As the number of nodes in the tree increases, the number of steps grows roughly the same way.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 visits to nodes |
| 100 | About 100 visits to nodes |
| 1000 | About 1000 visits to nodes |
Pattern observation: The work grows linearly with the number of nodes because each node is visited once.
Time Complexity: O(n)
This means the time to find the left side view grows directly in proportion to the number of nodes in the tree.
[X] Wrong: "The time complexity is O(log n) because we only look at one node per level."
[OK] Correct: Even though we record one node per level, we still visit all nodes to find that first node. So the total work depends on all nodes, not just one per level.
Understanding this helps you explain how tree traversals work and how to analyze their efficiency, a common skill interviewers look for.
"What if we changed the traversal to depth-first search instead of breadth-first? How would the time complexity change?"
Practice
Left Side View of a binary tree represent?Solution
Step 1: Understand the view perspective
The left side view shows nodes visible when looking at the tree from the left side.Step 2: Identify visible nodes
Only the first node at each level from the left is visible in this view.Final Answer:
Nodes visible when the tree is viewed from the left side -> Option DQuick Check:
Left side view = nodes visible from left [OK]
- Confusing left side view with right side view
- Thinking it shows all leaf nodes
- Assuming it shows deepest nodes only
Solution
Step 1: Identify the correct data structure
We need a list to store nodes in order, so an empty list is best.Step 2: Check syntax for list initialization
Using square brackets[]creates an empty list in Python.Final Answer:
left_view = [] -> Option CQuick Check:
Empty list = [] [OK]
- Using {} which creates a dictionary, not a list
- Using () which creates a tuple, not a list
- Using set() which is unordered
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))Solution
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).Step 2: Confirm nodes added only once per level
Right children at each level are ignored if left child exists at that level.Final Answer:
[1, 2, 4] -> Option BQuick Check:
Left side view nodes = [1, 2, 4] [OK]
- Adding right child nodes before left child nodes
- Appending nodes multiple times per level
- Confusing left and right children order
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 resultSolution
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.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.Final Answer:
The condition should be 'level == len(result)', not 'level < len(result)' -> Option AQuick Check:
Use '==' to add first node per level [OK]
- Using '<' instead of '==' in condition
- Returning result inside recursive function
- Swapping left and right child calls unnecessarily
Solution
Step 1: Perform standard left side view traversal
Use DFS or BFS to collect the first node at each level from left to right.Step 2: Reverse the collected list
After traversal, reverse the list to get nodes from bottom to top.Final Answer:
Perform the usual left side view traversal, then reverse the result list before returning -> Option AQuick Check:
Reverse after left side view traversal [OK]
- Changing traversal order instead of reversing result
- Collecting leaf nodes only
- Using right to left traversal which gives wrong nodes
Ready to prove it?
Pass the test and this pattern moves unknown
