| 16 | { |
| 17 | public: |
| 18 | Node* lca(Node* root ,int n1 ,int n2 ) |
| 19 | { |
| 20 | if(root == NULL || root->data == n1 || root->data == n2) { |
| 21 | return root; |
| 22 | } |
| 23 | |
| 24 | Node* left = lca(root->left, n1, n2); |
| 25 | Node* right = lca(root->right, n1, n2); |
| 26 | |
| 27 | if(left != NULL && right != NULL) { |
| 28 | return root; |
| 29 | } else if (left != NULL) { |
| 30 | return left; |
| 31 | } else { |
| 32 | return right; |
| 33 | } |
| 34 | } |
| 35 | }; |