LeetCode(算法)- 236. 二叉树的最近公共祖先

Posted 放羊的牧码

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了LeetCode(算法)- 236. 二叉树的最近公共祖先相关的知识,希望对你有一定的参考价值。

题目链接:点击打开链接

题目大意:

解题思路:

相关企业

  • 字节跳动
  • Facebook
  • 亚马逊(Amazon)
  • 谷歌(Google)
  • 微软(Microsoft)
  • 苹果(Apple)
  • 领英(LinkedIn)
  • PayPal

AC 代码

  • Java
/**
 * Definition for a binary tree node.
 * public class TreeNode 
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x)  val = x; 
 * 
 */

// 解决方案(1)
class Solution 

    Queue<TreeNode> pQue = new LinkedList<>(), qQue = new LinkedList<>();

    Map<TreeNode, Integer> pMap = new HashMap<>(), qMap = new HashMap<>();

    public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) 
        dfs(root, p.val, pQue, pMap, 0);
        dfs(root, q.val, qQue, qMap, 0);
        TreeNode res = common();
        return res;
    

    TreeNode common() 
        TreeNode res;
        while (!pQue.isEmpty() && !qQue.isEmpty()) 
            TreeNode pe = pQue.peek();
            TreeNode qe = qQue.peek();
            if (pe.val == qe.val) 
                res = pQue.peek();
                pQue.poll();
                qQue.poll();
                return res;
            

            if (pMap.get(pe) > qMap.get(qe)) 
                pQue.poll();
             else if (pMap.get(pe) < qMap.get(qe)) 
                qQue.poll();
             else 
                pQue.poll();
                qQue.poll();
            
        
        return null;
    

    boolean dfs(TreeNode node, int val, Queue<TreeNode> queue, Map<TreeNode, Integer> map, int l) 
        if (node == null) 
            return false;
        

        if (node.val == val) 
            queue.offer(node);
            map.put(node, l);
            return true;
        

        boolean left = dfs(node.left, val, queue, map, l + 1);
        if (left) 
            queue.offer(node);
            map.put(node, l);
            return true;
        
        boolean right = dfs(node.right, val, queue, map, l + 1);
        if (right) 
            queue.offer(node);
            map.put(node, l);
            return true;
        

        return false;
    


// 解决方案(2)
class Solution 
    public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) 
        if(root == null || root == p || root == q) return root;
        TreeNode left = lowestCommonAncestor(root.left, p, q);
        TreeNode right = lowestCommonAncestor(root.right, p, q);
        if(left == null) return right;
        if(right == null) return left;
        return root;
    
  • 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* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) 
        if(root == nullptr || root == p || root == q) return root;
        TreeNode *left = lowestCommonAncestor(root->left, p, q);
        TreeNode *right = lowestCommonAncestor(root->right, p, q);
        if(left == nullptr) return right;
        if(right == nullptr) return left;
        return root;
    
;

以上是关于LeetCode(算法)- 236. 二叉树的最近公共祖先的主要内容,如果未能解决你的问题,请参考以下文章

LeetCode 236. 二叉树的最近公共祖先

LeetCode236. 二叉树的最近公共祖先

236. 二叉树的最近公共祖先

leetcode 236 二叉树的最近公共祖先

leetcode-236二叉树的最近公共祖先

leetcode 236. 二叉树的最近公共祖先