/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
TreeNode* ans = NULL;
TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {
if(!root) return NULL;
if((root->val-p->val)*(root->val-q->val)<=0)
{
ans = root;
}
else if(root->val > p->val) lowestCommonAncestor(root->left,p,q);
else lowestCommonAncestor(root->right,p,q);
return ans;
}
};