美文网首页
235. Lowest Common Ancestor of a

235. Lowest Common Ancestor of a

作者: larrymusk | 来源:发表于2017-11-20 22:58 被阅读0次

代码思路和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

      本文链接:https://www.haomeiwen.com/subject/lnnbvxtx.html