235. Lowest Common Ancestor of a Binary Search Tree

代码思路和Lowest Common Ancestor of a Binary Tree 一模一样

 struct TreeNode* lowestCommonAncestor(struct TreeNode* root, struct TreeNode* p, struct TreeNode* q) {
    if(root == NULL || root == p || root == q)
        return root;

    struct TreeNode *left = lowestCommonAncestor(root->left, p,q);
    struct TreeNode *right = lowestCommonAncestor(root->right, p,q);

    if(left&&right)
        return root;
    if(left)
        return left;
    if(right)
        return right;

    return NULL;
    
}

你可能感兴趣的:(235. Lowest Common Ancestor of a Binary Search Tree)