Leetcode-236. Lowest Common Ancestor of a Binary Tree 最小公共祖先

这篇博客探讨了如何在给定的二叉树中寻找两个节点的最低公共祖先(LCA)。根据定义,LCA是树中最低的节点,同时是两个指定节点的祖先。文章提到了两种解决思路,一种是找到两个节点到根节点的路径并比较最后一个公共节点,另一种思路没有详细说明。文章指出,对于其中一个节点不在树中的特殊情况,需要在方法中进行适当的处理,并且这种方法也可用于求解二叉树中两个节点之间的最短路径问题。
摘要由CSDN通过智能技术生成

Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree.

According to the definition of LCA on Wikipedia: “The lowest common ancestor is defined between two nodes v and w as the lowest node in T that has both v and w as descendants (where we allow a node to be a descendant of itself).”

        _______3______
       /              \
    ___5__          ___1__
   /      \        /      \
   6      _2       0       8
         /  \
         7   4

For example, the lowest common ancestor (LCA) of nodes 5 and 1 is 3. Another example is LCA of nodes 5 and 4 is 5, since a node can be a descendant of itself according to the LCA definition.

二叉树的经典问题,也是笔试面试常考问题。


两种思路:第一种比较好想,就是找到两个节点p,q对应的root->....->p和root->....->q的路径,然后比较两个路径,找到最后一公共节点即可。

class Solution {
public:
    TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {
        if(!root || root == p || root == q) return root;
        vector<TreeNode*> s,t;
        findPath(root, p, s);
        findPath(root, q, t);
        int k = min(s.size(), t.size()), i = 1;
        for(;i<k;++i){
            if(s[i] != t[i]) break;
        } 
        return s[i-1];
    }
    bool findPath(TreeNode* root, TreeNode* node, vector<TreeNode*>& path){
        if (root == NULL)
            return false;
        path.push_back(root);
        if (root == node)
            return true;
        if (root->left && findPath(root->left, node, path) || 
            root->right && findPath(root->right, node, path))
            return true;
        path.pop_back();
        return false;
    }
};

第二种思路:递归寻找。如果p,q节点有一个是当前root节点,那就直接返回root节点,如果不是,就在左右子树中寻找p,q。如果在一个子树中找到p,另个子树找到q,那么当前root就是最小的公共祖先。如果都在同一个子树上,那就递归在这个子树中寻找p,q的公共祖先。代码非常简单。

class Solution {
public:
    TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {
        if (!root || root == p || root == q) return root;
        TreeNode* left = lowestCommonAncestor(root->left, p, q);
        TreeNode* right = lowestCommonAncestor(root->right, p, q);
        return !left ? right : !right ? left : root;
    }
};


后:不知道p,q中如果有一个点不在树中,这种情况有没有考虑过。好像方法二没对这种情况进行讨论。方法一的话对应bool的函数进行一个判断即可。另外方法一的话可以解决二叉树中两个节点之间最短路径这类问题



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值