0
0
DSA Javascriptprogramming~10 mins

Left Side View of Binary Tree in DSA Javascript - Execution Trace

Choose your learning style9 modes available
Concept Flow - Left Side View of Binary Tree
Start at root node
Initialize queue with root
While queue not empty
Get number of nodes at current level
For each node in level
Dequeue node
If first node in level, add to left view
Enqueue left child if exists
Enqueue right child if exists
Repeat for all nodes in level
Repeat for next level
End when queue empty
Return collected left view nodes
We start from the root and explore the tree level by level. At each level, we record the first node we see (the leftmost). We use a queue to keep track of nodes to visit next.
Execution Sample
DSA Javascript
function leftSideView(root) {
  if (!root) return [];
  const queue = [root];
  const leftView = [];
  while (queue.length) {
    const levelSize = queue.length;
    for (let i = 0; i < levelSize; i++) {
      const currentNode = queue.shift();
      if (i === 0) leftView.push(currentNode.val);
      if (currentNode.left) queue.push(currentNode.left);
      if (currentNode.right) queue.push(currentNode.right);
    }
  }
  return leftView;
}
This code finds the left side view of a binary tree by traversing level by level and recording the first node at each level.
Execution Table
StepOperationNode VisitedQueue StateLeft ViewVisual State
1Initialize queue with root1[1][]1
2Start level 1, levelSize=1-[1][]1
3Dequeue node1[][]1
4First node at level, add to left view1[][1]1
5Enqueue left child2[2][1]1 -> 2
6Enqueue right child3[2,3][1]1 -> 2, 3
7Start level 2, levelSize=2-[2,3][1]1 -> 2, 3
8Dequeue node2[3][1]1 -> 2, 3
9First node at level, add to left view2[3][1,2]1 -> 2, 3
10Enqueue left child4[3,4][1,2]1 -> 2 -> 4, 3
11Enqueue right child5[3,4,5][1,2]1 -> 2 -> 4, 3 -> 5
12Dequeue node3[4,5][1,2]1 -> 2 -> 4, 3 -> 5
13Not first node, no add3[4,5][1,2]1 -> 2 -> 4, 3 -> 5
14Enqueue left childnull[4,5][1,2]No left child
15Enqueue right child6[4,5,6][1,2]1 -> 2 -> 4, 3 -> 5 -> 6
16Start level 3, levelSize=3-[4,5,6][1,2]1 -> 2 -> 4, 3 -> 5 -> 6
17Dequeue node4[5,6][1,2]1 -> 2 -> 4, 3 -> 5 -> 6
18First node at level, add to left view4[5,6][1,2,4]1 -> 2 -> 4, 3 -> 5 -> 6
19Enqueue left childnull[5,6][1,2,4]No left child
20Enqueue right childnull[5,6][1,2,4]No right child
21Dequeue node5[6][1,2,4]1 -> 2 -> 4, 3 -> 5 -> 6
22Not first node, no add5[6][1,2,4]1 -> 2 -> 4, 3 -> 5 -> 6
23Enqueue left childnull[6][1,2,4]No left child
24Enqueue right childnull[6][1,2,4]No right child
25Dequeue node6[][1,2,4]1 -> 2 -> 4, 3 -> 5 -> 6
26Not first node, no add6[][1,2,4]1 -> 2 -> 4, 3 -> 5 -> 6
27Enqueue left childnull[][1,2,4]No left child
28Enqueue right childnull[][1,2,4]No right child
29Queue empty, end-[][1,2,4]Final left view: 1 -> 2 -> 4
💡 Queue is empty, all nodes visited, left view collected
Variable Tracker
VariableStartAfter Step 5After Step 11After Step 15After Step 18After Step 25Final
queue[1][2,3][3,4,5][4,5,6][5,6][][]
leftView[][1][1,2][1,2][1,2,4][1,2,4][1,2,4]
currentNode123456-
Key Moments - 3 Insights
Why do we add the node to the left view only when it is the first node at its level?
Because the left side view shows the leftmost node visible at each level. The first node dequeued at each level is the leftmost. See execution_table rows 4, 9, and 18 where the first node at each level is added.
Why do we use a queue to traverse the tree level by level?
A queue helps us visit nodes in the order they appear in each level, from left to right. This ensures we process all nodes at one level before moving to the next. See execution_table rows 2, 7, and 16 showing queue states at each level start.
What happens if a node has no left or right child?
We simply do not enqueue anything for that child. The queue size decreases naturally as we finish nodes. See execution_table rows 14, 19, 23, and 27 where no children are enqueued.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the leftView array after step 9?
A[1]
B[1,2]
C[1,2,4]
D[]
💡 Hint
Check the Left View column at step 9 in the execution_table.
At which step does the queue become empty, ending the traversal?
AStep 25
BStep 27
CStep 29
DStep 18
💡 Hint
Look at the Queue State column and the exit_note in the execution_table.
If the root node had no left child, how would the queue state change after step 5?
A[3]
B[2,3]
C[1]
D[]
💡 Hint
See step 5 where left child is enqueued; if missing, only right child remains in queue.
Concept Snapshot
Left Side View of Binary Tree:
- Traverse tree level by level using a queue
- At each level, record the first node visited
- Enqueue left child first, then right child
- Continue until all levels processed
- Result is list of leftmost nodes per level
Full Transcript
To find the left side view of a binary tree, we start at the root and use a queue to visit nodes level by level. At each level, we note the first node we visit because it is the leftmost visible node from that side. We add this node to our left view list. We then enqueue the left child and right child of each node to process the next level. We repeat this until the queue is empty, meaning all nodes have been visited. The final list shows the nodes visible from the left side of the tree.