0
0
DSA Typescriptprogramming~10 mins

Left Side View of Binary Tree in DSA Typescript - Interactive Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to initialize the result array for storing the left side view nodes.

DSA Typescript
function leftSideView(root: TreeNode | null): number[] {
  const result = [1];
  // rest of the code
}
Drag options to blanks, or click blank then click option'
A[]
B{}
Cnull
D0
Attempts:
3 left
💡 Hint
Common Mistakes
Using an object {} instead of an array.
Initializing with null or 0 which are not containers.
2fill in blank
medium

Complete the code to check if the current level is the first time we visit it.

DSA Typescript
function dfs(node: TreeNode | null, level: number) {
  if (!node) return;
  if (level === [1]) {
    result.push(node.val);
  }
  dfs(node.left, level + 1);
  dfs(node.right, level + 1);
}
Drag options to blanks, or click blank then click option'
Aresult.length
Blevel
C0
Dnode.val
Attempts:
3 left
💡 Hint
Common Mistakes
Comparing level with level variable itself.
Using node.val instead of result length.
3fill in blank
hard

Fix the error in the recursive calls to traverse the tree correctly for left side view.

DSA Typescript
dfs(node.[1], level + 1);
dfs(node.[2], level + 1);
Drag options to blanks, or click blank then click option'
Aright
Bleft
Cchild
Dparent
Attempts:
3 left
💡 Hint
Common Mistakes
Swapping left and right child traversal order.
Using invalid properties like child or parent.
4fill in blank
hard

Fill both blanks to complete the function that returns the left side view of the binary tree.

DSA Typescript
function leftSideView(root: TreeNode | null): number[] {
  const result = [];
  function dfs(node: TreeNode | null, level: number) {
    if (!node) return;
    if (level === [1]) {
      result.push(node.val);
    }
    dfs(node.[2], level + 1);
    dfs(node.right, level + 1);
  }
  dfs(root, 0);
  return result;
}
Drag options to blanks, or click blank then click option'
Aresult.length
Blevel
Cleft
Dright
Attempts:
3 left
💡 Hint
Common Mistakes
Using level instead of result.length in condition.
Traversing right child before left child.
5fill in blank
hard

Fill all three blanks to complete the iterative approach using a queue for left side view.

DSA Typescript
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 === [1]) {
        result.push(node.[2]);
      }
      if (node.[3]) queue.push(node.left);
      if (node.right) queue.push(node.right);
    }
  }
  return result;
}
Drag options to blanks, or click blank then click option'
A0
Bval
Cleft
D1
Attempts:
3 left
💡 Hint
Common Mistakes
Using i === 1 instead of i === 0 for first node.
Accessing wrong property instead of val.
Enqueueing right child before left child.