0
0
DSA Typescriptprogramming~10 mins

Left Side View of Binary Tree in DSA Typescript - 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 at this level
Dequeue node
Enqueue left child if exists
Enqueue right child if exists
Repeat for next level
Return collected left side view nodes
We start from the root and use a queue to traverse the tree level by level. For each level, we record the first node encountered as part of the left side view.
Execution Sample
DSA Typescript
function leftSideView(root: TreeNode | null): number[] {
  if (!root) return [];
  const queue = [root];
  const leftView = [];
  while (queue.length) {
    const levelSize = queue.length;
    for (let i = 0; i < levelSize; i++) {
      const node = queue.shift()!;
      if (i === 0) leftView.push(node.val);
      if (node.left) queue.push(node.left);
      if (node.right) queue.push(node.right);
    }
  }
  return leftView;
}
This code collects the left side view nodes of a binary tree using level order traversal.
Execution Table
StepOperationNode VisitedQueue StateLeft View CollectedVisual State
1Initialize queue with root1[1][]1 / \ 2 3 / \ 4 5
2Start level traversal, levelSize=11[1][]1 / \ 2 3 / \ 4 5
3Visit first node at level, add to left view1[][1]1 / \ 2 3 / \ 4 5
4Enqueue left child of 11[2][1]1 / \ 2 3 / \ 4 5
5Enqueue right child of 11[2,3][1]1 / \ 2 3 / \ 4 5
6Start level traversal, levelSize=22[2,3][1]1 / \ 2 3 / \ 4 5
7Visit first node at level, add to left view2[3][1,2]1 / \ 2 3 / \ 4 5
8Enqueue left child of 22[3,4][1,2]1 / \ 2 3 / \ 4 5
9No right child of 22[3,4][1,2]1 / \ 2 3 / \ 4 5
10Visit next node at level3[4][1,2]1 / \ 2 3 / \ 4 5
11No left child of 33[4][1,2]1 / \ 2 3 / \ 4 5
12Enqueue right child of 33[4,5][1,2]1 / \ 2 3 / \ 4 5
13Start level traversal, levelSize=24[4,5][1,2]1 / \ 2 3 / \ 4 5
14Visit first node at level, add to left view4[5][1,2,4]1 / \ 2 3 / \ 4 5
15No left child of 44[5][1,2,4]1 / \ 2 3 / \ 4 5
16No right child of 44[5][1,2,4]1 / \ 2 3 / \ 4 5
17Visit next node at level5[][1,2,4]1 / \ 2 3 / \ 4 5
18No left child of 55[][1,2,4]1 / \ 2 3 / \ 4 5
19No right child of 55[][1,2,4]1 / \ 2 3 / \ 4 5
20Queue empty, traversal ends-[][1,2,4]1 / \ 2 3 / \ 4 5
💡 Queue is empty, all levels processed, left side view collected
Variable Tracker
VariableStartAfter Step 4After Step 6After Step 13Final
queue[1][2][2,3][4,5][]
leftView[][1][1][1,2][1,2,4]
levelSize-122-
Key Moments - 3 Insights
Why do we add only the first node of each level to the left view?
Because the first node visited at each level is the leftmost node visible from the left side. This is shown in execution_table rows 3, 7, and 14 where the first node at each level is added to leftView.
Why do we use a queue for traversal?
A queue helps us visit nodes level by level (breadth-first). This ensures we process all nodes at one level before moving to the next, as seen in the queue state changes in execution_table.
What happens if the tree is empty?
The function returns an empty array immediately (see execution_sample code line 2). No traversal happens and leftView remains empty.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the leftView collected after visiting node 2 at step 7?
A[]
B[1]
C[1,2]
D[1,2,4]
💡 Hint
Check the 'Left View Collected' column at step 7 in execution_table
At which step does the queue become empty, ending the traversal?
AStep 17
BStep 20
CStep 19
DStep 13
💡 Hint
Look for the step where 'Queue State' is empty and operation notes traversal ends
If node 2 had a right child, how would the queue state change after step 8?
AQueue would include the right child after node 4
BQueue would remain the same as [3,4]
CQueue would only have the right child
DQueue would be empty
💡 Hint
Refer to steps 4 and 5 where children are enqueued after visiting a node
Concept Snapshot
Left Side View of Binary Tree:
- Use level order traversal with a queue
- For each level, record the first node visited
- Enqueue left then right children
- Continue until queue is empty
- Result is nodes visible from left side
Full Transcript
This visualization shows how 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, the first node visited is added to the left view list. We enqueue children left first, then right, to ensure leftmost nodes come first. The process repeats until all nodes are visited and the queue is empty. The final left view contains the nodes visible from the left side of the tree.