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 Javascript
function leftSideView(root) {
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 result as null or 0 which cannot hold multiple nodes.
✗ 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 visited for the first time.
DSA Javascript
function dfs(node, level) {
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 itself instead of result length.
Using node.val in the condition which is incorrect.
✗ Incorrect
We compare the current level with result.length to add the first node at each level.
3fill in blank
hardFix the error in the DFS traversal order to get the correct left side view.
DSA Javascript
function dfs(node, level) {
if (!node) return;
if (level === result.length) {
result.push(node.val);
}
dfs(node.[1], level + 1);
dfs(node.right, level + 1);
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Visiting right child before left child causes wrong nodes in left view.
Using invalid property names like parent or child.
✗ Incorrect
We must visit the left child first to capture the leftmost nodes at each level.
4fill in blank
hardFill both blanks to complete the iterative approach using a queue for left side view.
DSA Javascript
function leftSideView(root) {
if (!root) return [];
const result = [];
const queue = [root];
while (queue.length > 0) {
const size = queue.length;
for (let i = 0; i < size; i++) {
const node = queue.shift();
if (i === [1]) {
result.push(node.val);
}
if (node.[2]) queue.push(node.[2]);
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 to pick the first node.
Enqueuing right child before left child changes the order.
✗ Incorrect
We add the first node at each level (i === 0) and enqueue left child first for left side view.
5fill in blank
hardFill all three blanks to complete the recursive left side view function with helper.
DSA Javascript
function leftSideView(root) {
const result = [];
function helper(node, level) {
if (!node) return;
if (level === [1]) {
result.push(node.[2]);
}
helper(node.[3], level + 1);
helper(node.right, level + 1);
}
helper(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.
Pushing node instead of node.val.
Recursing right child before left child.
✗ Incorrect
We check if level equals result.length, push node.val, and recurse left child first.