Bird
Raised Fist0
DSA Typescriptprogramming~20 mins

Left Side View of Binary Tree in DSA Typescript - Practice Problems & Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Left Side View Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of Left Side View for a Simple Binary Tree
What is the output of the following TypeScript code that prints the left side view of a binary tree?
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 | null)[] = [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);
      if (node.left) queue.push(node.left);
      if (node.right) queue.push(node.right);
    }
  }
  return result;
}

const root = new TreeNode(1, new TreeNode(2, new TreeNode(4), null), new TreeNode(3));
console.log(leftSideView(root));
A[1, 2, 4]
B[1, 3, 4]
C[1, 2, 3]
D[1, 4]
Attempts:
2 left
💡 Hint
Remember, the left side view shows the first node visible at each level from the left side.
🧠 Conceptual
intermediate
1:30remaining
Understanding Left Side View Node Selection
In the left side view of a binary tree, which nodes are included in the output?
AThe rightmost node at each level
BAll leaf nodes
CThe leftmost node at each level
DAll nodes at the deepest level
Attempts:
2 left
💡 Hint
Think about what you see when looking at the tree from the left side.
🔧 Debug
advanced
2:00remaining
Identify the Bug in Left Side View Implementation
What error will the following TypeScript code produce when trying to get the left side view of a binary tree?
DSA Typescript
function leftSideView(root: TreeNode | null): number[] {
  if (!root) return [];
  const result: number[] = [];
  const queue: (TreeNode | null)[] = [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);
      if (node.left) queue.push(node.left);
      if (node.right) queue.push(node.right);
    }
  }
  return result;
}
ARuntime error: Cannot read property 'val' of undefined
BSyntax error: Unexpected token
COutput is missing some nodes but no error
DCorrect output with no errors
Attempts:
2 left
💡 Hint
Check the loop condition carefully and how many times you remove nodes from the queue.
🚀 Application
advanced
2:00remaining
Left Side View of an Unbalanced Binary Tree
Given the following unbalanced binary tree, what is the left side view output?
DSA Typescript
const root = new TreeNode(10,
  new TreeNode(5, null, new TreeNode(7, new TreeNode(6), null)),
  new TreeNode(15, null, new TreeNode(20))
);
console.log(leftSideView(root));
A[10, 15, 20]
B[10, 5, 6, 20]
C[10, 5, 7]
D[10, 5, 7, 6]
Attempts:
2 left
💡 Hint
Remember to pick the first node at each level from left to right.
❓ Predict Output
expert
2:30remaining
Output of Left Side View for Complex Tree with Nulls
What is the output of the following TypeScript code that prints the left side view of a binary tree with some null children?
DSA Typescript
const root = new TreeNode(1,
  new TreeNode(2, null, new TreeNode(5, new TreeNode(7), null)),
  new TreeNode(3, null, new TreeNode(4))
);
console.log(leftSideView(root));
A[1, 2, 7, 4]
B[1, 2, 5, 7]
C[1, 3, 4, 7]
D[1, 2, 5, 4]
Attempts:
2 left
💡 Hint
Trace each level and pick the first node encountered from left to right.

Practice

(1/5)
1. What does the Left Side View of a binary tree represent?
easy
A. Nodes visible when the tree is viewed from the right side
B. Nodes at the deepest level of the tree
C. All leaf nodes of the tree
D. Nodes visible when the tree is viewed from the left side

Solution

  1. Step 1: Understand the view perspective

    The left side view shows nodes visible when looking at the tree from the left side.
  2. Step 2: Identify visible nodes

    Only the first node at each level from the left is visible in this view.
  3. Final Answer:

    Nodes visible when the tree is viewed from the left side -> Option D
  4. Quick Check:

    Left side view = nodes visible from left [OK]
Hint: Left side view shows first node at each level from left [OK]
Common Mistakes:
  • Confusing left side view with right side view
  • Thinking it shows all leaf nodes
  • Assuming it shows deepest nodes only
2. Which of the following TypeScript function signatures correctly represents a function to get the left side view of a binary tree?
easy
A. function leftSideView(root: number[]): number
B. function leftSideView(root: TreeNode | null): number[]
C. function leftSideView(root: TreeNode): void
D. function leftSideView(root: string): string[]

Solution

  1. Step 1: Identify correct input type

    The function should accept a binary tree root node or null, so TreeNode | null is correct.
  2. Step 2: Identify correct return type

    The function returns an array of numbers representing node values visible from the left side.
  3. Final Answer:

    function leftSideView(root: TreeNode | null): number[] -> Option B
  4. Quick Check:

    Input = TreeNode or null, Output = number array [OK]
Hint: Input is tree root or null; output is array of numbers [OK]
Common Mistakes:
  • Using array of numbers as input instead of tree node
  • Returning void instead of array
  • Using wrong input type like string
3. Given the binary tree below, what is the left side view output?
    1
   / \
  2   3
   \   \
    4   5

Consider the TypeScript code:
console.log(leftSideView(root));
medium
A. [1, 2, 4]
B. [1, 3, 5]
C. [1, 2, 5]
D. [1, 4, 5]

Solution

  1. Step 1: Identify nodes visible from left at each level

    Level 1: Node 1 is visible.
    Level 2: Node 2 is the leftmost node.
    Level 3: Node 4 is visible from left (child of 2).
  2. Step 2: Collect visible nodes in order

    The left side view nodes are [1, 2, 4].
  3. Final Answer:

    [1, 2, 4] -> Option A
  4. Quick Check:

    Left side nodes = [1, 2, 4] [OK]
Hint: Pick first node at each level from left [OK]
Common Mistakes:
  • Choosing right child nodes instead of left
  • Including nodes not visible from left
  • Confusing order of nodes
4. The following TypeScript code attempts to find the left side view of a binary tree but has a bug. What is the bug?
function leftSideView(root: TreeNode | null): number[] {
  const result: number[] = [];
  function dfs(node: TreeNode | null, level: number) {
    if (!node) return;
    if (result.length === level) {
      result.push(node.val);
    }
    dfs(node.right, level + 1);
    dfs(node.left, level + 1);
  }
  dfs(root, 0);
  return result;
}
medium
A. The result array is not initialized
B. The base case is missing to stop recursion
C. The function visits right child before left, so left side nodes are missed
D. The function does not return any value

Solution

  1. Step 1: Analyze traversal order

    The code visits the right child before the left child, so it records right side nodes first.
  2. Step 2: Understand impact on left side view

    Because right nodes are visited first, left side nodes at each level may be skipped, causing incorrect output.
  3. Final Answer:

    The function visits right child before left, so left side nodes are missed -> Option C
  4. Quick Check:

    Visit left child first for correct left side view [OK]
Hint: Traverse left child before right to capture left side nodes [OK]
Common Mistakes:
  • Visiting right child before left child
  • Forgetting base case (though present here)
  • Not pushing node values at correct level
5. You want to find the left side view of a binary tree but only have the tree nodes stored in a nested object format without parent references. Which approach is best to implement in TypeScript to get the left side view efficiently?
hard
A. Use level order traversal (BFS) and pick the first node at each level
B. Use in-order traversal and collect nodes in an array
C. Use post-order traversal and reverse the result array
D. Use a stack to perform iterative right-to-left traversal

Solution

  1. Step 1: Understand traversal types and their suitability

    Level order traversal (BFS) visits nodes level by level, making it easy to pick the first node at each level.
  2. Step 2: Evaluate other traversal methods

    In-order and post-order traversals do not guarantee level-wise order. Right-to-left iterative traversal misses left side nodes.
  3. Final Answer:

    Use level order traversal (BFS) and pick the first node at each level -> Option A
  4. Quick Check:

    BFS level order = best for left side view [OK]
Hint: Use BFS and select first node per level for left side view [OK]
Common Mistakes:
  • Using in-order or post-order which don't give level order
  • Traversing right to left instead of left to right
  • Ignoring level boundaries in traversal
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard