0
0
DSA Typescriptprogramming~20 mins

Left Side View of Binary Tree in DSA Typescript - 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 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.