0
0
DSA Typescriptprogramming

Left Side View of Binary Tree in DSA Typescript

Choose your learning style9 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 and only seeing the first window on each floor from your view.
      1
     / \
    2   3
   / \   \
  4   5   6

Left side view: 1 -> 2 -> 4 -> null
Dry Run Walkthrough
Input: Binary tree: 1 -> 2, 3 -> 4, 5, null, 6 (levels: root=1, level2=2,3, level3=4,5,null,6)
Goal: Find the leftmost node at each level to form the left side view list
Step 1: Start at root node 1, add it to left side view
Level 1: [1↑]
Left side view: 1 -> null
Why: Root is always visible from the left side
Step 2: Move to level 2, nodes 2 and 3; add leftmost node 2 to view
Level 2: [2↑] -> 3
Left side view: 1 -> 2 -> null
Why: At level 2, node 2 is the first visible from left
Step 3: Move to level 3, nodes 4, 5, null, 6; add leftmost node 4 to view
Level 3: [4↑] -> 5 -> null -> 6
Left side view: 1 -> 2 -> 4 -> null
Why: At level 3, node 4 is the first visible from left
Result:
1 -> 2 -> 4 -> null
Annotated Code
DSA Typescript
class TreeNode {
  val: number;
  left: TreeNode | null;
  right: TreeNode | null;
  constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
    this.val = val === undefined ? 0 : val;
    this.left = left === undefined ? null : left;
    this.right = right === undefined ? null : right;
  }
}

function leftSideView(root: TreeNode | null): number[] {
  if (!root) return [];
  const result: number[] = [];
  const queue: TreeNode[] = [root];

  while (queue.length > 0) {
    const levelSize = queue.length;
    for (let i = 0; i < levelSize; i++) {
      const node = queue.shift()!;
      if (i === 0) {
        result.push(node.val); // first node at this level is visible from left
      }
      if (node.left) queue.push(node.left); // add left child for next level
      if (node.right) queue.push(node.right); // add right child for next level
    }
  }
  return result;
}

// Driver code to build tree and print left side view
const root = new TreeNode(1,
  new TreeNode(2,
    new TreeNode(4),
    new TreeNode(5)
  ),
  new TreeNode(3,
    null,
    new TreeNode(6)
  )
);

const view = leftSideView(root);
console.log(view.join(' -> ') + ' -> null');
if (!root) return [];
handle empty tree edge case
const queue: TreeNode[] = [root];
initialize queue for level order traversal
const levelSize = queue.length;
determine number of nodes at current level
const node = queue.shift()!;
remove node from front of queue to process
if (i === 0) { result.push(node.val); }
add first node of level to left side view
if (node.left) queue.push(node.left);
enqueue left child for next level
if (node.right) queue.push(node.right);
enqueue right child for next level
OutputSuccess
1 -> 2 -> 4 -> null
Complexity Analysis
Time: O(n) because we visit each node once during level order traversal
Space: O(n) because the queue can hold up to all nodes at the widest level
vs Alternative: Compared to recursive DFS, this BFS approach naturally processes nodes level by level, making it easier to pick the first node at each level without extra depth tracking.
Edge Cases
Empty tree (root is null)
Returns empty list since no nodes exist
DSA Typescript
if (!root) return [];
Tree with only root node
Returns list with only root value
DSA Typescript
if (i === 0) { result.push(node.val); }
Tree with missing left children at some levels
Still picks first node at each level, which may be a right child
DSA Typescript
if (i === 0) { result.push(node.val); }
When to Use This Pattern
When asked to find the visible nodes from one side of a tree, use level order traversal and pick the first node at each level to get the left side view.
Common Mistakes
Mistake: Trying to do this with simple preorder traversal without tracking levels
Fix: Use level order traversal (BFS) to process nodes level by level and pick the first node at each level
Mistake: Adding all nodes at a level instead of only the first
Fix: Add only the first node encountered at each level (i === 0) to the result
Summary
Finds the first visible node at each level when looking at the tree from the left side.
Use when you need to see the leftmost nodes of a binary tree level by level.
The key insight is to do a level order traversal and pick the first node at each level.