Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to visit the left subtree first in inorder traversal.
DSA Javascript
function inorder(node) {
if (node === null) return;
inorder(node.[1]);
console.log(node.value);
inorder(node.right);
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'right' instead of 'left' causes wrong traversal order.
Using 'root' or 'parent' properties which do not exist.
✗ Incorrect
In inorder traversal, we first visit the left child of the current node.
2fill in blank
mediumComplete the code to print the current node's value in inorder traversal.
DSA Javascript
function inorder(node) {
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 'left' or 'right' instead of the node's value.
Trying to print 'parent' which is not defined.
✗ Incorrect
We print the current node's value after visiting the left subtree.
3fill in blank
hardFix the error in the inorder traversal to visit the right subtree correctly.
DSA Javascript
function inorder(node) {
if (node === null) return;
inorder(node.left);
console.log(node.value);
inorder(node.[1]);
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'left' instead of 'right' causes wrong traversal.
Using 'root' or 'parent' which are invalid here.
✗ Incorrect
After visiting the root, inorder traversal visits the right child.
4fill in blank
hardFill both blanks to create a function that returns an array of inorder traversal values.
DSA Javascript
function inorderArray(node) {
if (node === null) return [];
return [...inorderArray(node.[1]), node.[2], ...inorderArray(node.right)];
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Swapping left and right causes wrong order.
Using 'parent' or 'root' which are invalid.
✗ Incorrect
We collect values from left subtree, then current node's value, then right subtree.
5fill in blank
hardFill all three blanks to create a function that builds a map of node values to their inorder positions.
DSA Javascript
function inorderMap(node, map = new Map(), index = {count: 0}) {
if (node === null) return map;
inorderMap(node.[1], map, index);
map.set(node.[2], index.[3]);
index.count++;
inorderMap(node.right, map, index);
return map;
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'right' instead of 'left' in first blank.
Using 'parent' or 'root' which do not exist.
Using 'index' instead of 'count' for the counter.
✗ Incorrect
We traverse left subtree, map node's value to current count, then increment count and traverse right subtree.