#include <iostream>
using namespace std;
struct Node {
int data;
Node* left;
Node* right;
};
Node* findLCA(Node* root, int n1, int n2) {
if (root == NULL)
return NULL;
if (root->data == n1 || root->data == n2)
return root;
Node* leftLCA = findLCA(root->left, n1, n2);
Node* rightLCA = findLCA(root->right, n1, n2);
if (leftLCA && rightLCA)
return root;
return (leftLCA != NULL) ? leftLCA : rightLCA;
}
int main() {
Node* root = new Node();
root->data = 10;
root->left = new Node();
root->left->data = 20;
root->right = new Node();
root->right->data = 30;
root->left->left = new Node();
root->left->left->data = 40;
root->left->right = new Node();
root->left->right->data = 50;
root->right->left = new Node();
root->right->left->data = 60;
root->right->right = new Node();
root->right->right->data = 70;
Node* lca = findLCA(root, 40, 70);
cout << lca->data << endl;
return 0;
}
What will be the output of the above code?
Correct answer is option 'B'. Can you explain this answer?