Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to check if the current node is null.
DSA C#
if (root == [1]) return null;
Attempts:
3 left
💡 Hint
Common Mistakes
Using 0 instead of null.
Using true or false which are boolean values.
✗ Incorrect
We check if the current node is null to stop recursion.
2fill in blank
mediumComplete the code to check if the current node matches either of the target nodes.
DSA C#
if (root == p || root == [1]) return root;
Attempts:
3 left
💡 Hint
Common Mistakes
Checking against p twice.
Checking against null or unrelated variables.
✗ Incorrect
We return the current node if it matches either p or q.
3fill in blank
hardFix the error in the recursive calls to search left and right subtrees.
DSA C#
var left = LowestCommonAncestor(root.[1], p, q); var right = LowestCommonAncestor(root.[2], p, q);
Attempts:
3 left
💡 Hint
Common Mistakes
Using incorrect property names like leftChild or rightChild.
Using uppercase letters which are not standard.
✗ Incorrect
The standard property names for child nodes are 'left' and 'right'.
4fill in blank
hardFill both blanks to return the correct node based on left and right subtree results.
DSA C#
if (left != null && right != null) return [1]; return [2] != null ? [2] : left;
Attempts:
3 left
💡 Hint
Common Mistakes
Returning left or right when both are non-null.
Returning null when one side is non-null.
✗ Incorrect
If both sides are non-null, root is the ancestor; otherwise return the non-null side.
5fill in blank
hardFill all three blanks to complete the method signature and return type.
DSA C#
public [1] LowestCommonAncestor([2] root, [3] p, [3] q) { // method body }
Attempts:
3 left
💡 Hint
Common Mistakes
Using void as return type.
Using int instead of TreeNode for parameters.
✗ Incorrect
The method returns a TreeNode and takes TreeNode parameters for root, p, and q.
