Complete the code to check if the current node is null.
if (root [1] null) return null;
We check if root is exactly equal to null to stop recursion.
Complete the code to check if the current node is either p or q.
if (root [1] p || root [1] q) return root;
We compare root with p and q using strict equality.
Fix the error in the recursive calls to search left and right subtrees.
const left = lowestCommonAncestor(root.[1], p, q); const right = lowestCommonAncestor(root.[2], p, q);
The left subtree is accessed by root.left and the right subtree by root.right.
Fill both blanks to return the correct node based on left and right subtree results.
if (left [1] null && right [2] null) return root;
If both left and right are not null, the current root is the lowest common ancestor.
Fill the blank to return the non-null subtree or null if both are null.
return left [1] right;
We use logical OR || to return the non-null subtree or null if both are null.
