Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to return the left child of a node in a BST.
DSA Javascript
function getLeftChild(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 get the left child.
✗ Incorrect
The left child of a BST node is accessed using the 'left' property.
2fill in blank
mediumComplete the code to move to the right child of a node in a BST.
DSA Javascript
function getRightChild(node) {
return node.[1];
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'left' instead of 'right' to get the right child.
✗ Incorrect
The right child of a BST node is accessed using the 'right' property.
3fill in blank
hardFix the error in the code to find the inorder predecessor when the left subtree exists.
DSA Javascript
function inorderPredecessor(node) {
let current = node.[1];
while (current.right !== null) {
current = current.right;
}
return current;
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Starting from node.right instead of node.left.
✗ Incorrect
To find the inorder predecessor, start from the left child and go right as far as possible.
4fill in blank
hardFill both blanks to check if a node has a left child and then find its rightmost child.
DSA Javascript
if (node.[1] !== null) { let current = node.[2]; while (current.right !== null) { current = current.right; } return current; }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using different properties for check and traversal.
✗ Incorrect
Check if node.left exists, then start from node.left to find the rightmost node.
5fill in blank
hardFill all three blanks to complete the function that finds the inorder predecessor when no left child exists.
DSA Javascript
function inorderPredecessor(node) {
if (node.left !== null) {
let current = node.left;
while (current.right !== null) {
current = current.right;
}
return current;
} else {
let current = node;
let parent = node.[1];
while (parent !== null && current === parent.[2]) {
current = parent;
parent = parent.[3];
}
return parent;
}
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'left' instead of 'right' in the while condition.
Not moving up the parent chain correctly.
✗ Incorrect
When no left child, move up using parent pointers until current is not the right child of parent.