Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using an object {} instead of an array.
Initializing with null or 0 which are not containers.
✗ Incorrect
We use an empty array [] to store the nodes visible from the left side.
2fill in blank
mediumComplete 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Comparing level with level variable itself.
Using node.val instead of result length.
✗ Incorrect
We compare the current level with the length of result to add the first node at that level.
3fill in blank
hardFix 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Swapping left and right child traversal order.
Using invalid properties like child or parent.
✗ Incorrect
We first traverse the left child, then the right child to get the left side view.
4fill in blank
hardFill 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using level instead of result.length in condition.
Traversing right child before left child.
✗ Incorrect
We check if level equals result.length and traverse left child first.
5fill in blank
hardFill 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'
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.
✗ Incorrect
We add the first node at each level (i === 0), push its value, and enqueue left child first.