Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to check if a node is a leaf node.
DSA Typescript
function isLeaf(node: TreeNode | null): boolean {
return node !== null && node.left === null && node.[1] === null;
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Checking only left child for null.
Using incorrect property names like 'value' or 'data'.
✗ Incorrect
A leaf node has no left or right children, so both must be null.
2fill in blank
mediumComplete the code to traverse the left boundary excluding leaf nodes.
DSA Typescript
function addLeftBoundary(root: TreeNode | null, res: number[]): void {
let curr = root?.left;
while (curr !== null) {
if (!isLeaf(curr)) res.push(curr.val);
curr = curr.left !== null ? curr.left : curr.[1];
}
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Moving to parent or next node which is not part of boundary.
Using node value instead of node reference.
✗ Incorrect
If left child is null, move to right child to continue left boundary traversal.
3fill in blank
hardFix the error in the code to add leaf nodes in left-to-right order.
DSA Typescript
function addLeaves(root: TreeNode | null, res: number[]): void {
if (root === null) return;
if (isLeaf(root)) {
res.push(root.val);
return;
}
addLeaves(root.[1], res);
addLeaves(root.right, res);
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Traversing right subtree before left subtree.
Using incorrect property names.
✗ Incorrect
To add leaves in left-to-right order, traverse left subtree first, then right subtree.
4fill in blank
hardFill both blanks to add the right boundary nodes in bottom-up order excluding leaf nodes.
DSA Typescript
function addRightBoundary(root: TreeNode | null, res: number[]): void {
let curr = root?.right;
const stack: number[] = [];
while (curr !== null) {
if (!isLeaf(curr)) stack.push(curr.val);
curr = curr.[1] !== null ? curr.[1] : curr.[2];
}
while (stack.length > 0) {
res.push(stack.pop()!);
}
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Going left first instead of right.
Not using a stack to reverse order.
✗ Incorrect
Traverse right boundary by going right if possible, else left child.
5fill in blank
hardFill all three blanks to complete the boundary traversal function.
DSA Typescript
function boundaryTraversal(root: TreeNode | null): number[] {
const res: number[] = [];
if (root === null) return res;
if (!isLeaf(root)) res.push(root.val);
addLeftBoundary(root, res);
addLeaves(root, res);
addRightBoundary(root, res);
return res;
}
// Helper function to check leaf
function isLeaf(node: TreeNode | null): boolean {
return node !== null && node.left === null && node.[1] === null;
}
// Add left boundary
function addLeftBoundary(root: TreeNode | null, res: number[]): void {
let curr = root?.left;
while (curr !== null) {
if (!isLeaf(curr)) res.push(curr.val);
curr = curr.left !== null ? curr.left : curr.[2];
}
}
// Add leaves
function addLeaves(root: TreeNode | null, res: number[]): void {
if (root === null) return;
if (isLeaf(root)) {
res.push(root.val);
return;
}
addLeaves(root.left, res);
addLeaves(root.right, res);
}
// Add right boundary
function addRightBoundary(root: TreeNode | null, res: number[]): void {
let curr = root?.right;
const stack: number[] = [];
while (curr !== null) {
if (!isLeaf(curr)) stack.push(curr.val);
curr = curr.right !== null ? curr.right : curr.[3];
}
while (stack.length > 0) {
res.push(stack.pop()!);
}
}
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing left and right child in traversal.
Incorrect leaf node check.
✗ Incorrect
Leaf check uses right child; left boundary goes left then right; right boundary goes right then left.