https://leetcode-cn.com/problems/lowest-common-ancestor-of-a-binary-search-tree/
//注意是二叉搜索树
class Solution {
public:
TreeNode* ans;
void dfs(TreeNode* root, TreeNode* p, TreeNode* q){
if(root->val > p->val && root->val > q->val){
ans = root;
dfs(root->left, p, q);
}else if(root->val < p->val && root->val < q->val){
ans = root;
dfs(root->right, p, q);
}else{
ans = root;
return ;
}
}
TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {
dfs(root, p, q);
return ans;
}
};