0
0
DSA Javascriptprogramming~20 mins

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

Choose your learning style9 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.