struct Node {
int val;
Node* left;
Node* right;
Node(int x) : val(x), left(nullptr), right(nullptr) {}
};
Node* findLCA(Node* root, int n1, int n2) {
if (!root) return nullptr;
if (root->val == n1 || root->val == n2) return root;
Node* left_lca = findLCA(root->left, n1, n2);
Node* right_lca = findLCA(root->right, n1, n2);
if (left_lca && right_lca) return root;
return (left_lca != nullptr) ? left_lca : right_lca;
}
int main() {
Node* root = new Node(3);
root->left = new Node(5);
root->right = new Node(1);
root->left->left = new Node(6);
root->left->right = new Node(2);
root->right->left = new Node(0);
root->right->right = new Node(8);
root->left->right->left = new Node(7);
root->left->right->right = new Node(4);
Node* lca = findLCA(root, 7, 4);
if (lca) std::cout << lca->val << std::endl;
else std::cout << "No LCA found" << std::endl;
return 0;
}The nodes 7 and 4 are both children of node 2. The function finds that node 2 is the lowest node in the tree that has both 7 and 4 as descendants. Hence, the output is 2.
The base case returns the current node when it matches one of the target nodes. This signals that one target node has been found, which helps the recursion identify the ancestor nodes.
Node* findLCA(Node* root, int n1, int n2) { if (!root) return nullptr; if (root->val == n1 || root->val == n2) return root; Node* left_lca = findLCA(root->left, n1, n2); Node* right_lca = findLCA(root->right, n1, n2); if (left_lca && right_lca) return root; if (left_lca) return left_lca; if (right_lca) return right_lca; // Missing return statement here return nullptr; }
The function lacks a return statement if both left_lca and right_lca are nullptr. This causes a compilation error because the function expects a return value on all paths.
struct Node {
int val;
Node* left;
Node* right;
Node(int x) : val(x), left(nullptr), right(nullptr) {}
};
Node* findLCA(Node* root, int n1, int n2) {
if (!root) return nullptr;
if (root->val == n1 || root->val == n2) return root;
Node* left_lca = findLCA(root->left, n1, n2);
Node* right_lca = findLCA(root->right, n1, n2);
if (left_lca && right_lca) return root;
return (left_lca != nullptr) ? left_lca : right_lca;
}
int main() {
Node* root = new Node(3);
root->left = new Node(5);
root->right = new Node(1);
root->left->left = new Node(6);
root->left->right = new Node(2);
root->right->left = new Node(0);
root->right->right = new Node(8);
root->left->right->left = new Node(7);
root->left->right->right = new Node(4);
Node* lca = findLCA(root, 5, 10);
if (lca) std::cout << lca->val << std::endl;
else std::cout << "No LCA found" << std::endl;
return 0;
}The function returns the node that it finds first (5). Since 10 is not in the tree, the function returns 5 as the LCA, which is the node found.
The recursive calls return nodes upward when they find one of the target nodes. When both left and right recursive calls return non-null, it means the current node is the lowest node where paths to both targets split, identifying the LCA without needing parent pointers.