Complete the code to check if the current node is None.
if root [1] None: return None
We check if the current node is exactly None to stop recursion.
Complete the code to check if the current node is either p or q.
if root == [1] or root == q: return root
We check if root matches node p or q to return it as a potential ancestor.
Fix the error in the recursive call to search the left subtree.
left = [1](root.left, p, q)The function calls itself recursively by its own name to search the left subtree.
Fill both blanks to return the ancestor found in left or right subtree.
if left is not None and right is not None: return [1] return [2] if left is not None else right
If both left and right are found, root is the lowest common ancestor. Otherwise, return whichever subtree found a node.
Fill all three blanks to complete the recursive function for lowest common ancestor.
def lowestCommonAncestor(root, p, q): if root [1] None: return None if root == p or root == q: return root left = lowestCommonAncestor(root.left, p, q) right = lowestCommonAncestor(root.right, p, q) if left is not None and right is not None: return [2] return [3] if left is not None else right
The function checks if root is None, returns root if it matches p or q, recursively searches left and right, returns root if both sides found nodes, else returns the found side.
