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 Java
TreeNode leftChild = node.[1];Attempts:
3 left
💡 Hint
Common Mistakes
Using 'right' instead of 'left' to access the left child.
Confusing 'parent' with child nodes.
✗ Incorrect
The left child of a node in a BST is accessed using 'left'.
2fill in blank
mediumComplete the code to find the maximum node in the left subtree.
DSA Java
while (current.[1] != null) { current = current.[1]; }
Attempts:
3 left
💡 Hint
Common Mistakes
Going left instead of right to find maximum.
Stopping traversal too early.
✗ Incorrect
To find the maximum node in a subtree, keep going right until null.
3fill in blank
hardFix the error in the code to move up to the parent node.
DSA Java
current = current.[1];Attempts:
3 left
💡 Hint
Common Mistakes
Using 'left' or 'right' to move up the tree.
Using 'root' which is not a pointer in the node.
✗ Incorrect
To move up in the tree, use the 'parent' pointer.
4fill in blank
hardFill both blanks to check if current node is a right child of its parent.
DSA Java
if (current == current.[1].[2]) { current = current.[1]; }
Attempts:
3 left
💡 Hint
Common Mistakes
Confusing left and right child checks.
Not accessing the parent before checking children.
✗ Incorrect
We check if current is the right child of its parent by comparing with parent's right.
5fill in blank
hardFill all three blanks to complete the inorder predecessor function.
DSA Java
public TreeNode inorderPredecessor(TreeNode node) {
if (node.[1] != null) {
TreeNode current = node.[1];
while (current.[2] != null) {
current = current.[2];
}
return current;
}
TreeNode current = node;
TreeNode parent = node.[3];
while (parent != null && current == parent.[2]) {
current = parent;
parent = parent.[3];
}
return parent;
}Attempts:
3 left
💡 Hint
Common Mistakes
Using 'left' instead of 'right' when finding max in left subtree.
Not moving up correctly using parent pointers.
✗ Incorrect
The inorder predecessor is the maximum node in left subtree or the nearest ancestor for which node is in right subtree.
