```
class Solution {
public:
TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {
if (!root)
return NULL;
if (root==p ||root==q)
return root;
TreeNode* n1 = lowestCommonAncestor(root->left, p, q);
TreeNode* n2 = lowestCommonAncestor(root->right, p, q);
if (n1 && n2)
return root;
if (n1)
return n1;
return n2;
}
};
```