Leetcode 236. 二叉树的最近公共祖先(DAY 10)


原题题目


在这里插入图片描述



代码实现(首刷自解)

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     struct TreeNode *left;
 *     struct TreeNode *right;
 * };
 */

struct TreeNode* ans;

bool visit(struct TreeNode* root, struct TreeNode* p, struct TreeNode* q)
{
    if(!root)
        return false;
    bool left = visit(root->left,p,q);
    bool right = visit(root->right,p,q);

    if((root == p || root == q) && (left || right) || left && right)
    {
        ans = root;
        return true;
    }

    if(root == p || root == q)
        return true;

    return left || right;
}
struct TreeNode* lowestCommonAncestor(struct TreeNode* root, struct TreeNode* p, struct TreeNode* q) {
    ans = NULL;
    visit(root,p,q);
    return ans;
}

代码实现(二刷自解 C++)

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:

    TreeNode* ret;
    bool visit(TreeNode* p,TreeNode* q,TreeNode* root)
    {
        if(!root)  return false;
        bool left = visit(p,q,root->left);
        bool right = visit(p,q,root->right);
        if(root == p || root == q)
        {
            if(left || right)   ret = root;
            return true;
        }
        else if(left && right)  
            ret = root; 
        return left || right;
    } 

    TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {
        ret = nullptr;
        if(p == root  || q == root) return root;
        if(visit(p,q,root->left) && !ret)   return root;
        else  visit(p,q,root->right);
        return ret;
    }
};

代码实现(三刷自解 DAY 146 C++)


/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    TreeNode* ret = nullptr;

    bool visit(TreeNode* root,TreeNode* p,TreeNode* q)
    {
        if(!root)   return false;
        bool left = visit(root->left,p,q);
        bool right = visit(root->right,p,q);
        bool judge = (root->val == p->val || root->val == q->val);
        if(right && left || ((right || left) && judge))   ret = root;
        return left || right || judge;
    }

    TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {
        if(root->val == p->val || root->val == q->val)  return root;
        visit(root->left,p,q);
        visit(root->right,p,q);
        if(!ret) ret = root;
        return ret;
    }
};

代码实现(四刷自解 DAY 209 C++)


/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    bool helper(TreeNode* root,TreeNode* p,TreeNode* q,TreeNode** ret)
    {
        if(!root)   return false;
        bool left  = helper(root->left,p,q,ret);
        bool right = helper(root->right,p,q,ret);

        bool judge = (root == p || root == q);

        if(judge && (left || right) || left && right)    *ret = root;

        return (left || right || judge);
    }

    TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {
        if(root == p || root == q)  return root;
        TreeNode* left_ancestor = nullptr,*right_ancestor = nullptr;

        helper(root->left,p,q,&left_ancestor);
        helper(root->right,p,q,&right_ancestor);

        if(left_ancestor)     return left_ancestor;
        if(right_ancestor)    return right_ancestor;  

        return root;
    }
};

代码实现(五刷自解 DAY 270 C++)


/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    TreeNode* ret = nullptr;

    bool Visit(TreeNode* root, TreeNode* target) {
      if (!root) return false;
      if (target == root) return true;

      return Visit(root->left, target) || Visit(root->right, target);
    }

    bool FindAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {
      if (!root) return false;
      
      bool left = FindAncestor(root->left, p, q);
      bool right = FindAncestor(root->right, p, q);
      if (ret) return true;
      
      bool has = (root == p || root == q);
      if ((left && right) || has && (left || right))  ret = root;
      
      return has || left || right;
    }

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

        bool p_in_left = Visit(root->left, p);
        bool q_in_left = Visit(root->left, q);
        if (p_in_left && !q_in_left || !p_in_left && q_in_left) return root;

        if(p_in_left) {
          FindAncestor(root->left, p, q); 
        } else {
          FindAncestor(root->right, p, q);
        }
        return ret;
    }
};

代码实现(六刷自解 DAY 2 Golang)


/**
 * Definition for a binary tree node.
 * type TreeNode struct {
 *     Val int
 *     Left *TreeNode
 *     Right *TreeNode
 * }
 */

var ret *TreeNode

func hasroot(root, other *TreeNode) bool {
  if root == nil || other == nil {
    return false
  }

  return root.Val == other.Val || hasroot(root.Left, other) || hasroot(root.Right, other)
}

func helper(root, p, q *TreeNode) bool {
  if root == nil {
    return false
  }

  left, right := helper(root.Left, p, q), helper(root.Right, p, q)
  judge := root.Val == p.Val || root.Val == q.Val

  if left && right || (left || right) && judge {
    ret = root
    return true
  } 

  return left || right || judge
}

func lowestCommonAncestor(root, p, q *TreeNode) *TreeNode {
  if root == q || root == p {
    return root
  }

  if hasroot(root.Left, p) && hasroot(root.Right, q) ||
     hasroot(root.Left, q) && hasroot(root.Right, p) {
       return root
    }

  helper(root, p, q)
  return ret
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Love 6

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值