Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to visit the left subtree first in inorder traversal.
DSA Typescript
function inorder(node: TreeNode | null): void {
if (node === null) return;
inorder(node[1]);
console.log(node.val);
inorder(node.right);
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using node.right instead of node.left for the first recursive call.
Trying to access node.val instead of a child node.
✗ Incorrect
In inorder traversal, we first visit the left child, so we call inorder(node.left).
2fill in blank
mediumComplete the code to print the root node value in inorder traversal.
DSA Typescript
function inorder(node: TreeNode | null): void {
if (node === null) return;
inorder(node.left);
console.log(node[1]);
inorder(node.right);
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Printing node.left or node.right instead of node.val.
Trying to print node.parent which may not exist.
✗ Incorrect
In inorder traversal, after visiting left subtree, we print the root node's value using node.val.
3fill in blank
hardFix the error in the inorder traversal recursive call for the right subtree.
DSA Typescript
function inorder(node: TreeNode | null): void {
if (node === null) return;
inorder(node.left);
console.log(node.val);
inorder(node[1]);
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Calling inorder(node.left) again instead of node.right.
Trying to call inorder(node.val) which is a value, not a node.
✗ Incorrect
The right subtree is visited by calling inorder(node.right) in inorder traversal.
4fill in blank
hardFill both blanks to complete the inorder traversal that collects values in an array.
DSA Typescript
function inorder(node: TreeNode | null, result: number[]): void {
if (node === null) return;
inorder(node[1], result);
result.push(node[2]);
inorder(node.right, result);
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using node.right in the first recursive call instead of node.left.
Pushing node.right or node.left instead of node.val.
✗ Incorrect
We first recurse on the left child (node.left), then push the root value (node.val) to the result array.
5fill in blank
hardFill all three blanks to complete inorder traversal that returns an array of values.
DSA Typescript
function inorder(node: TreeNode | null): number[] {
if (node === null) return [];
const left = inorder(node[1]);
const right = inorder(node[2]);
return [...left, node[3], ...right];
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Swapping left and right children in recursive calls.
Using node.parent or node.val in recursive calls instead of child nodes.
Returning node instead of node.val in the combined array.
✗ Incorrect
We recursively get left subtree values with node.left, right subtree values with node.right, and combine them with the root value node.val in the middle.