Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to create a new node with the given value.
DSA Typescript
class TreeNode { val: number; left: TreeNode | null; right: TreeNode | null; constructor(val: number) { this.val = [1]; this.left = null; this.right = null; } }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using a variable name not defined in the constructor.
Assigning 'this.val' to a wrong variable.
✗ Incorrect
The constructor parameter is named 'val', so we assign 'this.val = val;' to set the node's value.
2fill in blank
mediumComplete the code to serialize the binary tree using preorder traversal.
DSA Typescript
function serialize(root: TreeNode | null): string {
const result: string[] = [];
function dfs(node: TreeNode | null) {
if (node === null) {
result.push('null');
return;
}
result.push(node.val.toString());
dfs(node.[1]);
dfs(node.right);
}
dfs(root);
return result.join(',');
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Calling dfs on the right child before the left child.
Using an incorrect property name.
✗ Incorrect
In preorder traversal, we visit the left child before the right child, so we call dfs(node.left) first.
3fill in blank
hardFix the error in the deserialize function to correctly parse the next node value.
DSA Typescript
function deserialize(data: string): TreeNode | null {
const values = data.split(',');
let index = 0;
function dfs(): TreeNode | null {
if (values[[1]] === 'null') {
index++;
return null;
}
const node = new TreeNode(parseInt(values[index]));
index++;
node.left = dfs();
node.right = dfs();
return node;
}
return dfs();
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Checking a fixed index like 0 or 1 instead of the current index.
Using values.length which is out of bounds.
✗ Incorrect
We must check the current index position in the values array to see if it is 'null'.
4fill in blank
hardFill in the blank to correctly implement the base case in deserialize.
DSA Typescript
function deserialize(data: string): TreeNode | null {
const values = data.split(',');
let index = 0;
function dfs(): TreeNode | null {
if (values[index] [1] 'null') {
index++;
return null;
}
const node = new TreeNode(parseInt(values[index]));
index++;
node.left = dfs();
node.right = dfs();
return node;
}
return dfs();
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using '!=' or '!==', which would invert the condition.
Using comparison operators like '<' or '>'.
✗ Incorrect
The base case checks if the current value is exactly 'null' using '===' operator.
5fill in blank
hardFill all three blanks to complete the serialize function using preorder traversal with null markers.
DSA Typescript
function serialize(root: TreeNode | null): string {
const result: string[] = [];
function dfs(node: TreeNode | null) {
if (node === [1]) {
result.push([2]);
return;
}
result.push(node.val.toString());
dfs(node.left);
dfs(node.[3]);
}
dfs(root);
return result.join(',');
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Checking for undefined instead of null.
Pushing null instead of the string 'null'.
Recursing on the wrong child.
✗ Incorrect
We check if node is null, push the string 'null', and recurse on the right child.