0
0
DSA C++programming~10 mins

Left Side View of Binary Tree in DSA C++ - Execution Trace

Choose your learning style9 modes available
Concept Flow - Left Side View of Binary Tree
Start at root node
Use queue for level order traversal
For each level:
Visit nodes left to right
Record first node of level
Add children to queue
Repeat until queue empty
Output collected left side nodes
Traverse the tree level by level, record the first node at each level to get the left side view.
Execution Sample
DSA C++
struct Node {
  int val;
  Node* left;
  Node* right;
};

vector<int> leftSideView(Node* root) {
  // BFS level order, record first node per level
}
This code collects the leftmost node at each level of the binary tree using a queue.
Execution Table
StepOperationQueue StateVisited NodeLeft Side View ListVisual Tree State
1Start: enqueue root (1)[1]None[] 1 / \ 2 3 / \ \ 4 5 6
2Process level 1: dequeue 1, record 1[]1[1] 1 / \ 2 3 / \ \ 4 5 6
3Enqueue children of 1: 2, 3[2,3]None[1] 1 / \ 2 3 / \ \ 4 5 6
4Process level 2: dequeue 2, record 2[3]2[1,2] 1 / \ 2 3 / \ \ 4 5 6
5Enqueue children of 2: 4, 5[3,4,5]None[1,2] 1 / \ 2 3 / \ \ 4 5 6
6Process level 2: dequeue 3, do not record[4,5]3[1,2] 1 / \ 2 3 / \ \ 4 5 6
7Enqueue child of 3: 6[4,5,6]None[1,2] 1 / \ 2 3 / \ \ 4 5 6
8Process level 3: dequeue 4, record 4[5,6]4[1,2,4] 1 / \ 2 3 / \ \ 4 5 6
94 has no children to enqueue[5,6]None[1,2,4] 1 / \ 2 3 / \ \ 4 5 6
10Process level 3: dequeue 5, do not record[6]5[1,2,4] 1 / \ 2 3 / \ \ 4 5 6
115 has no children[6]None[1,2,4] 1 / \ 2 3 / \ \ 4 5 6
12Process level 3: dequeue 6, do not record[]6[1,2,4] 1 / \ 2 3 / \ \ 4 5 6
136 has no children[]None[1,2,4] 1 / \ 2 3 / \ \ 4 5 6
14Queue empty, traversal done[]None[1,2,4] 1 / \ 2 3 / \ \ 4 5 6
💡 Queue is empty, all levels processed, left side view collected.
Variable Tracker
VariableStartAfter Step 2After Step 4After Step 8After Step 12Final
Queue[][][3][5,6][][]
Visited NodeNone1246None
Left Side View List[][1][1,2][1,2,4][1,2,4][1,2,4]
Key Moments - 3 Insights
Why do we only record the first node at each level?
Because the first node visited at each level is the leftmost node visible from the left side. See execution_table rows 2, 4, and 8 where nodes 1, 2, and 4 are recorded.
Why do we continue processing other nodes at the same level after recording the first?
We need to enqueue all children of nodes at the current level to process the next level. The first node is recorded, but others are processed to add their children. See rows 6 and 10 where nodes 3 and 5 are processed but not recorded.
What happens if the tree is empty?
If the root is null, the queue starts empty and traversal ends immediately with an empty left side view list. This is implied by the exit condition in row 14.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 4, which node is recorded in the left side view list?
ANode 2
BNode 3
CNode 1
DNode 4
💡 Hint
Check the 'Left Side View List' column at step 4 in the execution_table.
At which step does the queue become empty, ending the traversal?
AStep 10
BStep 12
CStep 14
DStep 8
💡 Hint
Look at the 'Queue State' column in the execution_table and find when it is [].
If the node 2 had no left child, how would the left side view list change?
A[1, 3, 4]
B[1, 2, 5]
C[1, 2, 4]
D[1, 2, 6]
💡 Hint
Consider that without node 2's left child, the next leftmost node at level 3 would be node 5.
Concept Snapshot
Left Side View of Binary Tree:
- Traverse tree level by level (BFS).
- At each level, record the first node visited.
- Use a queue to track nodes per level.
- Enqueue children left to right.
- Result is nodes visible from left side.
Full Transcript
To find the left side view of a binary tree, we start at the root and use a queue to do a level order traversal. At each level, we visit nodes from left to right. We record only the first node we visit at each level because that node is visible from the left side. We continue until all levels are processed and the queue is empty. The recorded nodes form the left side view list. This method ensures we see the leftmost nodes at every depth of the tree.