Bird
Raised Fist0
DSA Javascriptprogramming~20 mins

Left Side View of Binary Tree in DSA Javascript - 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 code that prints the left side view of a binary tree?
DSA Javascript
class Node {
  constructor(val) {
    this.val = val;
    this.left = null;
    this.right = null;
  }
}

function leftSideView(root) {
  const result = [];
  function dfs(node, level) {
    if (!node) return;
    if (level === result.length) {
      result.push(node.val);
    }
    dfs(node.left, level + 1);
    dfs(node.right, level + 1);
  }
  dfs(root, 0);
  return result;
}

const root = new Node(1);
root.left = new Node(2);
root.right = new Node(3);
root.left.right = new Node(5);
root.right.right = new Node(4);

console.log(leftSideView(root));
A[1, 3, 5]
B[1, 3, 4]
C[1, 2, 5]
D[1, 2, 4]
Attempts:
2 left
💡 Hint
Think about which nodes are visible when looking from the left side, level by level.
❓ Predict Output
intermediate
2:00remaining
Left Side View Output for a Skewed Tree
What will be the output of the leftSideView function for this skewed binary tree?
DSA Javascript
class Node {
  constructor(val) {
    this.val = val;
    this.left = null;
    this.right = null;
  }
}

function leftSideView(root) {
  const result = [];
  function dfs(node, level) {
    if (!node) return;
    if (level === result.length) {
      result.push(node.val);
    }
    dfs(node.left, level + 1);
    dfs(node.right, level + 1);
  }
  dfs(root, 0);
  return result;
}

const root = new Node(10);
root.left = new Node(20);
root.left.left = new Node(30);
root.left.left.left = new Node(40);

console.log(leftSideView(root));
A[10, 20, 30, 40]
B[10, 20, 30]
C[10, 30, 40]
D[10, 20, 40]
Attempts:
2 left
💡 Hint
Since the tree is skewed to the left, all nodes should be visible from the left side.
🔧 Debug
advanced
2:00remaining
Identify the Bug in Left Side View Implementation
The following code is intended to return the left side view of a binary tree. What is the bug causing incorrect output?
DSA Javascript
function leftSideView(root) {
  const result = [];
  function dfs(node, level) {
    if (!node) return;
    dfs(node.left, level + 1);
    dfs(node.right, level + 1);
    if (level === result.length) {
      result.push(node.val);
    }
  }
  dfs(root, 0);
  return result;
}
AThe check for adding node.val to result is after recursive calls, causing right nodes to be added first.
BThe base case is missing, so the function runs infinitely.
CThe function uses BFS instead of DFS, which is incorrect for left side view.
DThe result array is not initialized before recursion.
Attempts:
2 left
💡 Hint
Think about when nodes are added to the result relative to recursion order.
🧠 Conceptual
advanced
1:30remaining
Understanding Left Side View with BFS
Which statement correctly describes how to get the left side view of a binary tree using breadth-first search (BFS)?
AAdd nodes only if they have a left child.
BAt each level, add the last node encountered in the queue to the result list.
CAdd all nodes at each level to the result list.
DAt each level, add the first node encountered in the queue to the result list.
Attempts:
2 left
💡 Hint
Think about which node is visible from the left side at each level.
🚀 Application
expert
2:30remaining
Left Side View with Missing Left Children
Given the binary tree below, what is the left side view output? Tree structure: - Root: 7 - Root.right: 9 - Root.right.left: 8 - Root.right.right: 10 Use the standard left side view logic (first node at each level from left to right).
A[7, 8, 10]
B[7, 9, 8]
C[7, 9, 10]
D[7, 8]
Attempts:
2 left
💡 Hint
Even if left child is missing, the first node at each level from left to right is included.

Practice

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

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 C
  4. Quick Check:

    Left side view = nodes visible from left [OK]
Hint: Think of standing on the left side and seeing nodes [OK]
Common Mistakes:
  • Confusing left side view with right side view
  • Including all leaf nodes instead of first nodes per level
  • Thinking it shows deepest nodes only
2. Which JavaScript data structure is best suited to help implement the left side view of a binary tree using level order traversal?
easy
A. Queue
B. Stack
C. Set
D. Map

Solution

  1. Step 1: Identify traversal method

    Level order traversal visits nodes level by level, left to right.
  2. Step 2: Choose data structure for level order

    A queue is used to process nodes in FIFO order, perfect for level order traversal.
  3. Final Answer:

    Queue -> Option A
  4. Quick Check:

    Level order traversal uses queue [OK]
Hint: Use queue for breadth-first level traversal [OK]
Common Mistakes:
  • Using stack which is LIFO, not suitable for level order
  • Choosing set or map which don't maintain order
  • Confusing queue with stack
3. What is the output of the following code that prints the left side view of the binary tree?
class Node {
  constructor(val) {
    this.val = val;
    this.left = null;
    this.right = null;
  }
}

function leftSideView(root) {
  if (!root) return [];
  const result = [];
  const queue = [root];
  while (queue.length) {
    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 Node(1);
root.left = new Node(2);
root.right = new Node(3);
root.left.left = new Node(4);
root.left.right = new Node(5);
root.right.right = new Node(6);

console.log(leftSideView(root));
medium
A. [1, 2, 5]
B. [1, 2, 4]
C. [1, 3, 6]
D. [1, 4, 6]

Solution

  1. Step 1: Traverse level by level and pick first node

    Level 1: node 1 (first node), Level 2: node 2, Level 3: node 4
  2. Step 2: Collect these nodes in result array

    Result array becomes [1, 2, 4]
  3. Final Answer:

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

    Left side nodes per level = [1, 2, 4] [OK]
Hint: Pick first node at each level during BFS [OK]
Common Mistakes:
  • Picking right child instead of left
  • Including all nodes instead of first per level
  • Confusing order of nodes
4. The following code attempts to print the left side view of a binary tree but produces incorrect output. What is the main bug?
function leftSideView(root) {
  if (!root) return [];
  const result = [];
  const queue = [root];
  while (queue.length) {
    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;
}
medium
A. The for loop condition should be i < levelSize, not i <= levelSize
B. The queue should be a stack for correct traversal
C. The check for i === 0 should be i === levelSize - 1
D. The function should return result.length instead of result

Solution

  1. Step 1: Analyze the for loop condition

    The loop runs from i = 0 to i <= levelSize, which means one extra iteration causing shift() on empty queue.
  2. Step 2: Correct the loop condition

    It should be i < levelSize to process exactly levelSize nodes per level.
  3. Final Answer:

    The for loop condition should be i < levelSize, not i <= levelSize -> Option A
  4. Quick Check:

    Loop must run exactly levelSize times [OK]
Hint: Use < not <= for loop over fixed size [OK]
Common Mistakes:
  • Using <= causes extra iteration and errors
  • Confusing queue with stack for BFS
  • Wrong index check for first node
5. Given a binary tree where some nodes only have right children, how would the left side view output look for this tree?
    1
     \
      2
       \
        3
         \
          4
hard
A. []
B. [1]
C. [4]
D. [1, 2, 3, 4]

Solution

  1. Step 1: Understand left side view with only right children

    Each level has only one node, which is the right child, but also the first node at that level.
  2. Step 2: Collect nodes visible from left side

    Since no left children, the right child nodes are visible from left side as they are the only nodes per level.
  3. Final Answer:

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

    Single node per level = all nodes visible [OK]
Hint: If no left child, right child is visible from left [OK]
Common Mistakes:
  • Assuming left side view is empty without left children
  • Ignoring right children as visible nodes
  • Thinking only left children count
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