Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to check if a node is a leaf node.
DSA Javascript
function isLeaf(node) {
return node.left === null && node.[1] === null;
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Checking only one child instead of both.
Using wrong property names like 'value' or 'parent'.
✗ Incorrect
A node is a leaf if both its left and right children are null. Here, we check if node.right is null.
2fill in blank
mediumComplete the code to add the left boundary nodes (excluding leaves) to the result array.
DSA Javascript
function addLeftBoundary(node, result) {
let curr = node.left;
while (curr !== null) {
if (!isLeaf(curr)) result.push(curr.value);
curr = curr.[1];
}
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Moving down right child instead of left.
Adding leaf nodes to the boundary.
✗ Incorrect
To traverse the left boundary, move down the left child nodes using curr.left.
3fill in blank
hardFix the error in the function that adds leaf nodes to the result array.
DSA Javascript
function addLeaves(node, result) {
if (node === null) return;
if (isLeaf(node)) {
result.push(node.[1]);
return;
}
addLeaves(node.left, result);
addLeaves(node.right, result);
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Pushing the left or right child instead of the node's value.
Using undefined properties like 'parent'.
✗ Incorrect
Leaf nodes hold their data in the 'value' property, which should be added to the result.
4fill in blank
hardFill both blanks to add the right boundary nodes (excluding leaves) in reverse order.
DSA Javascript
function addRightBoundary(node, result) {
let curr = node.[1];
const stack = [];
while (curr !== null) {
if (!isLeaf(curr)) stack.push(curr.value);
curr = curr.[2];
}
while (stack.length > 0) {
result.push(stack.pop());
}
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using left child to start the right boundary.
Not reversing the collected nodes before adding.
✗ Incorrect
Start from the right child and move down right children to collect right boundary nodes in reverse.
5fill in blank
hardFill the blank to complete the boundary traversal function.
DSA Javascript
function boundaryTraversal(root) {
if (root === null) return [];
const result = [root.[1]];
addLeftBoundary(root, result);
addLeaves(root.left, result);
addLeaves(root.right, result);
addRightBoundary(root, result);
return result;
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Adding leaves from root instead of its subtrees.
Wrong order of adding boundaries and leaves.
Forgetting to handle null root.
✗ Incorrect
Start with root's value, add left boundary and leaves from subtrees, then add right boundary to the result array.