Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to find the left child of a node in a BST.
DSA Javascript
function leftChild(node) {
return node[1];
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using '.right' instead of '.left' to access the left child.
Using '.parent' which points to the parent node, not the child.
✗ Incorrect
In a BST, the left child of a node is accessed using the '.left' property.
2fill in blank
mediumComplete the code to find the minimum node in a BST subtree.
DSA Javascript
function findMin(node) {
while (node[1]) {
node = node.left;
}
return node;
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Checking '.right' instead of '.left' in the loop condition.
Not updating the node inside the loop.
✗ Incorrect
To find the minimum node, keep going left until there is no left child.
3fill in blank
hardFix the error in the code to find the inorder successor when the node has no right child.
DSA Javascript
function inorderSuccessor(node) {
if (node.right) {
return findMin(node.right);
}
let parent = node[1];
while (parent && node === parent.right) {
node = parent;
parent = parent.parent;
}
return parent;
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using '.left' or '.right' instead of '.parent' to move up the tree.
Not updating the parent inside the loop.
✗ Incorrect
To move up the tree, we use the '.parent' property to find the parent node.
4fill in blank
hardFill both blanks to complete the function that finds the inorder successor of a node in a BST.
DSA Javascript
function inorderSuccessor(node) {
if (node.right) {
return [1](node.right);
}
let parent = node.[2];
while (parent && node === parent.right) {
node = parent;
parent = parent.parent;
}
return parent;
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'findMax' instead of 'findMin' for the right subtree.
Using 'right' or 'left' instead of 'parent' to move up.
✗ Incorrect
If the node has a right child, the successor is the minimum node in the right subtree (findMin). Otherwise, move up using the 'parent' property, which is accessed by 'node.parent'.
5fill in blank
hardFill all three blanks to complete the function that returns the inorder successor of a node in a BST.
DSA Javascript
function inorderSuccessor(node) {
if (node.[1]) {
return findMin(node.[2]);
}
let parent = node.[3];
while (parent && node === parent.right) {
node = parent;
parent = parent.parent;
}
return parent;
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using different properties for the first two blanks.
Using 'left' instead of 'parent' to move up.
✗ Incorrect
The function checks if the node has a right child (node.right). If yes, it finds the minimum node in the right subtree (node.right). Otherwise, it moves up the tree using the parent property (node.parent).