【leetcode】二叉搜索树的最近公共祖先

 

struct TreeNode* lowestCommonAncestor(struct TreeNode* root, struct TreeNode* p, struct TreeNode* q) {
    if(root == NULL){
        return NULL;
    }
    if(q->val > root->val && p->val > root->val){
        return lowestCommonAncestor(root->right , p, q);
    }else if(q->val < root->val && p->val < root->val){
        return lowestCommonAncestor(root->left , p, q);
    }
    return root;
}

 

posted @ 2020-09-21 17:30  温暖了寂寞  阅读(131)  评论(0编辑  收藏  举报