【LeetCode】236. Lowest Common Ancestor of a Binary Tree 二叉树的最近公共祖先(Medium)(JAVA)

本文介绍了解决二叉树中寻找两个指定节点最近公共祖先问题的两种方法。方法一通过中序遍历找到两节点间的最高层级节点;方法二递归地从根节点开始比较,高效地找到目标节点。

【LeetCode】236. Lowest Common Ancestor of a Binary Tree 二叉树的最近公共祖先(Medium)(JAVA)

题目地址: https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-tree/

题目描述:

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 p and q as the lowest node in T that has both p and q as descendants (where we allow a node to be a descendant of itself).”

Example 1:

Input: root = [3,5,1,6,2,0,8,null,null,7,4], p = 5, q = 1
Output: 3
Explanation: The LCA of nodes 5 and 1 is 3.

Example 2:

Input: root = [3,5,1,6,2,0,8,null,null,7,4], p = 5, q = 4
Output: 5
Explanation: The LCA of nodes 5 and 4 is 5, since a node can be a descendant of itself according to the LCA definition.

Example 3:

Input: root = [1,2], p = 1, q = 2
Output: 1

Constraints:

  • The number of nodes in the tree is in the range [2, 10^5].
  • -10^9 <= Node.val <= 10^9
  • All Node.val are unique.
  • p != q
  • p and q will exist in the tree.

题目大意

给定一个二叉树, 找到该树中两个指定节点的最近公共祖先。

百度百科中最近公共祖先的定义为:“对于有根树 T 的两个结点 p、q,最近公共祖先表示为一个结点 x,满足 x 是 p、q 的祖先且 x 的深度尽可能大(一个节点也可以是它自己的祖先)。”

解题方法

  1. 要找到两个节点最近的祖先节点,必须要先找到一个节点,然后再找到另一个节点,而且中序遍历时,祖先节点肯定是在这两个节点之间的
  2. 所以只要中序遍历找到两个节点,然后找两个节点之间的最高层级的节点即可
class Solution {

    TreeNode res;
    int resLevel;
    TreeNode p;
    TreeNode q;
    public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
        this.p = p;
        this.q = q;
        lH(root, 0);
        return res;
    }

    public void lH(TreeNode root, int level) {
        if (root == null || (p == null && q == null)) return;
        lH(root.left, level - 1);
        if (p == null && q == null) return;
        if (p == null || q == null || p == root || q == root) {
            if (res == null || level > resLevel) {
                res = root;
                resLevel = level;
            }
        }
        if (p == root) p = null;
        if (q == root) q = null;
        lH(root.right, level - 1);
    }
}

执行耗时:7 ms,击败了99.84% 的Java用户
内存消耗:40.6 MB,击败了72.16% 的Java用户

解法二
  1. 如果当前跟节点和两个节点中任意一个相同,那肯定就是结果了
  2. 如果不是,分别计算左边和右边的,看是不是有结果
  3. 如果左边和右边有一个是空的结果,那就在另一个那里
  4. 左右各一个,那就是当前根节点
class Solution {
    public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
        if (root == p || root == q || root == null) 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;
    }
}

执行耗时:7 ms,击败了99.84% 的Java用户
内存消耗:40.6 MB,击败了72.16% 的Java用户

欢迎关注我的公众号,LeetCode 每日一题更新
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值