236. Lowest Common Ancestor of a Binary Tree

本文介绍了一种寻找二叉树中两个指定节点的最低公共祖先(LCA)的方法。通过递归地检查每个节点及其子树,算法能够高效确定目标节点的公共祖先。提供了两种实现思路,一种使用辅助函数来标记节点路径,另一种采用更简洁的递归调用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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.
二刷 40%
思考:遍历到节点node,要判断它是否是p、q的LCA,想知道node是否等于p、q,以及node的左子树和右子树里包含p,q的情况。
建一个helper function,返回以node为根的树里包含p、q的情况。
     若返回1,表示包含p; 若返回2,表示包含q; 若返回3,表示包含p和q; 若返回0,表示都不包含。
遍历到node,可以分这几种情况:
     如果 node == p,再看node的左右子树里是否包含q,如果包含返回3,并且把node设为result
     如果 node == q,再看node的左右子树里是否包含p,如果包含返回3,并把node设为result
     如果 node != p 且node != q
         看node的左子树,如果返回3(包含p、q),则返回3(但不需要设置result, 因为result应该在root的左子树里)
         同理看node的右子树,如果返回3,则root也返回3
         如果helper(root.left) + helper(root.right) == 3,说明p和q一个在root的左子树里,一个在右子树里,把root设为result,返回3
     所有为3的情况都返回了,不为3的话返回helper(root.left) + helper(root.right)
/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
public class Solution {
    TreeNode node = null;
    public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
        findNode(root, p, q);
        return node;
    }
    public int findNode(TreeNode root, TreeNode p, TreeNode q) {
        if (root == null) {
            return 0;
        }
        int re = 0;
        if (root == p) {
            re = 1 + findNode(root.left, p, q) + findNode(root.right, p, q);
        } else if (root == q) {
            re = 2 + findNode(root.left, p, q) + findNode(root.right, p, q);
        } else {
            int left = findNode(root.left, p, q);
            if (left == 3) {
                return 3;
            }
            int right = findNode(root.right, p, q);
            if (right == 3) {
                return 3;
            }
            re = left + right;    
        }
        if (re == 3) {
            node = root;
        }
        return re;
    }
}

 recursively root,检查root是否是pq,root的左子树是否有pq,root的右子树是否有pq。 18%

如果左子树不含pq,lowestCommonAncestor(root.right,p,q)返回空,LCA在右子树中。
递归,检查root,检查root.left, 检查root.right

**这种办法是基于p、q一定在root中

 

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
public class Solution {
    public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
        if(root==null) return null;
        if(root==p || root==q) return root;
        TreeNode left=lowestCommonAncestor(root.left, p, q), right = lowestCommonAncestor(root.right, p, q);
        if(left!=null && right!=null) return root;
        if(right==null) return left;
        if(left==null) return right;
        return null;
    }
}

 

转载于:https://www.cnblogs.com/yuchenkit/p/7192628.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值