剑指Offer68-1. 二叉搜索树的最近公共祖先Java版

本文介绍如何在二叉搜索树中寻找两个指定节点的最近公共祖先,提供了三种实现思路及对应的Java代码:通过路径查找、迭代比对节点值和递归方法。

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

剑指Offer68-1. 二叉搜索树的最近公共祖先Java版

1. 问题描述

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

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

例如,给定如下二叉搜索树: root = [6,2,8,0,4,7,9,null,null,3,5]

示例 1:

输入: root = [6,2,8,0,4,7,9,null,null,3,5], p = 2, q = 8
输出: 6
解释: 节点 2 和节点 8 的最近公共祖先是 6。
示例 2:

输入: root = [6,2,8,0,4,7,9,null,null,3,5], p = 2, q = 4
输出: 2
解释: 节点 2 和节点 4 的最近公共祖先是 2, 因为根据定义最近公共祖先节点可以为节点本身。

说明:

所有节点的值都是唯一的。
p、q 为不同节点且均存在于给定的二叉搜索树中。

2. 思路

2.1. 思路1

  1. 编写函数,查找从root到指定节点p的路径
  2. 调用函数,找到节点p和节点q与root之间的路径
  3. 找到路径中相同的节点,就是祖先。

2.2. 思路2

  1. 把root作为两个节点的公共祖先
  2. 如果p和q都大于ancestor,ancestor = ancesstor.right
  3. 如果p和q都小于ancestor,ancestor = ancesstor.left

2.3. 思路3

递归

3. 代码

3.1. 思路1代码

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
class Solution {
    public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
        List<TreeNode> pathP, pathQ;
        pathP = getPath(root, p);
        pathQ = getPath(root, q);
        TreeNode ancestor = null;
        for (int i = 0; i < pathP.size()&& i <pathQ.size(); ++i) {
        	// 会找到最近的,如果是距离较远的那一个,会被较近的节点覆盖
            if (pathP.get(i) == pathQ.get(i)) {
                ancestor = pathP.get(i);
            } else {
                break;
            }
        }
        return ancestor;
    }

    private List<TreeNode> getPath(TreeNode root, TreeNode target) {
        List<TreeNode> path = new ArrayList<TreeNode>();
        TreeNode p = root;
        for(;p != target;) {
            path.add(p);
            if (target.val > p.val) {
                p = p.right;
            } else {
                p = p.left;
            }
        }
        path.add(p);
        return path;
    }
}

3.2. 思路2代码

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
class Solution {
    public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
        TreeNode ancestor = root;
        while(true) {
            if (p.val > ancestor.val && q.val > ancestor.val) {
                ancestor = ancestor.right;
            } else if (p.val < ancestor.val && q.val < ancestor.val) {
                ancestor = ancestor.left;
            } else {
                break;
            }
        }
        return ancestor;
    }
}

3.3. 思路3代码

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

        if (p.val > root.val && q.val > root.val) {
            return lowestCommonAncestor(root.right, p, q);
        }

        if (p.val < root.val && q.val < root.val) {
            return lowestCommonAncestor(root.left, p, q);
        }

        return root;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值