Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to return the left child of a node.
DSA Typescript
function getLeftChild(node: TreeNode | null): TreeNode | null {
return node[1];
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using .right instead of .left
Using .parent or .value which are not children
✗ Incorrect
The left child of a node in a BST is accessed using the .left property.
2fill in blank
mediumComplete the code to find the minimum node in a BST subtree.
DSA Typescript
function findMin(node: TreeNode | null): TreeNode | null {
while (node && node[1]) {
node = node.left;
}
return node;
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Checking node.right instead of node.left
Returning node without checking children
✗ Incorrect
To find the minimum node, keep going left until no left child exists.
3fill in blank
hardFix the error in the code to correctly find the inorder successor when the node has no right child.
DSA Typescript
function inorderSuccessor(root: TreeNode | null, p: TreeNode): TreeNode | null {
let successor: TreeNode | null = null;
while (root) {
if (p.value < root.value) {
successor = root;
root = root[1];
} else {
root = root.right;
}
}
return successor;
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Moving right instead of left when p.value < root.value
Using root.parent which is not available
✗ Incorrect
When p.value is less than root.value, move to root.left to find a smaller candidate successor.
4fill in blank
hardFill both blanks to complete the function that finds the inorder successor of a node in a BST.
DSA Typescript
function inorderSuccessor(root: TreeNode | null, p: TreeNode): TreeNode | null {
if (p.right) {
return findMin(p[1]);
}
let successor: TreeNode | null = null;
while (root) {
if (p.value < root.value) {
successor = root;
root = root[2];
} else {
root = root.right;
}
}
return successor;
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using p.left instead of p.right for right subtree
Moving root.right instead of root.left in the loop
✗ Incorrect
If p has a right child, successor is the minimum in right subtree (p.right). Otherwise, move left in root to find successor.
5fill in blank
hardFill all three blanks to complete the helper function and the inorder successor logic.
DSA Typescript
function findMin(node: TreeNode | null): TreeNode | null {
while (node && node[1]) {
node = node[2];
}
return node;
}
function inorderSuccessor(root: TreeNode | null, p: TreeNode): TreeNode | null {
if (p.right) {
return findMin(p[3]);
}
let successor: TreeNode | null = null;
while (root) {
if (p.value < root.value) {
successor = root;
root = root.left;
} else {
root = root.right;
}
}
return successor;
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using node.right instead of node.left in findMin
Using p.left instead of p.right for successor
✗ Incorrect
To find minimum, keep moving left while node.left exists. For successor, if p has right child, find minimum in p.right subtree.