class Solution {

public:

TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {

//int flag = 0;

int pn = 0;

int qn = 0;

if(!root || p == root || q == root ) return root;

```
RorL(root->right,p,pn);
RorL(root->right,q,qn);
if(pn != qn)
return root;
else if(pn==1 && qn==1)
return lowestCommonAncestor(root->right,p,q);
else
return lowestCommonAncestor(root->left,p,q);
}
void RorL(TreeNode* root,TreeNode* node,int &flag){
if(root == NULL) return ;
else{
TreeNode* tmp = root;
if(tmp->val == node->val){
flag = 1;
return ;
}
else{
RorL(tmp->left,node,flag);
RorL(tmp->right,node,flag);
}
}
return ;
}
```

};