Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to check if the current node is null.
DSA Java
if (root [1] null) return null;
Attempts:
3 left
💡 Hint
Common Mistakes
Using '=' instead of '==' causes assignment, not comparison.
Using '!=' would check for not null, which is opposite.
✗ Incorrect
We use '==' to compare if root is null in Java.
2fill in blank
mediumComplete the code to check if the current node matches either of the target nodes.
DSA Java
if (root [1] p || root [1] q) return root;
Attempts:
3 left
💡 Hint
Common Mistakes
Using '=' instead of '==' causes assignment, not comparison.
Using '!=' or '>' will not correctly check equality.
✗ Incorrect
We use '==' to check if root equals p or q.
3fill in blank
hardFix the error in the recursive calls to search left and right subtrees.
DSA Java
TreeNode left = lowestCommonAncestor(root.[1], p, q);
TreeNode right = lowestCommonAncestor(root.right, p, q);Attempts:
3 left
💡 Hint
Common Mistakes
Using 'right' instead of 'left' accesses the wrong subtree.
Using 'leftChild' or 'left.left' may not match the node definition.
✗ Incorrect
The left child of root is accessed by 'left' in this code context.
4fill in blank
hardFill both blanks to return the correct ancestor based on recursive results.
DSA Java
if (left != null && right != null) return [1]; return left != null ? [2] : right;
Attempts:
3 left
💡 Hint
Common Mistakes
Returning 'left' when both sides are non-null instead of root.
Returning 'right' instead of 'left' in the ternary operator.
✗ Incorrect
If both left and right are non-null, root is the ancestor; otherwise return the non-null child.
5fill in blank
hardFill all three blanks to complete the full method for Lowest Common Ancestor.
DSA Java
public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
if (root [1] null) return null;
if (root [2] p || root [2] q) return root;
TreeNode left = lowestCommonAncestor(root.[3], p, q);
TreeNode right = lowestCommonAncestor(root.right, p, q);
if (left != null && right != null) return root;
return left != null ? left : right;
}Attempts:
3 left
💡 Hint
Common Mistakes
Using '=' instead of '==' for comparisons.
Using '!=' instead of '==' for equality checks.
Using wrong field name for left child.
✗ Incorrect
Use '==' for comparisons and 'left' to access the left child node.
