Complete the code to check if the current node is null.
if (root == [1]) return nullptr;
We check if the root pointer is nullptr to know if we reached the end of a branch.
Complete the code to check if the current node matches either of the target nodes.
if (root->val == [1] || root->val == q->val || root->val == p->val) return root;
We check if the current node's value matches p->val or q->val. Here, the blank is for p->val.
Fix the error in the recursive calls to search left and right subtrees.
TreeNode* left = [1](root->left, p, q);
TreeNode* right = lowestCommonAncestor(root->right, p, q);The function calls itself recursively, so the function name must be lowestCommonAncestor.
Fill both blanks to return the correct ancestor based on left and right subtree results.
if (left != [1] && right != [2]) return root;
We check if both left and right are not nullptr, meaning both subtrees contain one of the nodes, so current root is the ancestor.
Fill all three blanks to return the non-null subtree result or nullptr if none found.
return left != [1] ? left : (right != [2] ? right : [3]);
We return left if it is not nullptr, else right if it is not nullptr, else nullptr.