【LeetCode】11. Lowest Common Ancestor of a Binary Search Tree· 二叉搜索树的最近公共祖先

有目标就不怕路远。年轻人.无论你现在身在何方.重要的是你将要向何处去。只有明确的目标才能助你成功。没有目标的航船.任何方向的风对他来说都是逆风。因此,再遥远的旅程,只要有目标.就不怕路远。没有目标,哪来的劲头?一车尔尼雷夫斯基

导读:本篇文章讲解 【LeetCode】11. Lowest Common Ancestor of a Binary Search Tree· 二叉搜索树的最近公共祖先,希望对大家有帮助,欢迎收藏,转发!站点地址:www.bmabk.com,来源:原文

题目描述

英文版描述

Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the BST. 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).”

英文版地址

https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-search-tree/

中文版描述

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

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

【LeetCode】11. Lowest Common Ancestor of a Binary Search Tree· 二叉搜索树的最近公共祖先

​示例 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 为不同节点且均存在于给定的二叉搜索树中

中文版地址

https://leetcode.cn/problems/lowest-common-ancestor-of-a-binary-search-tree/

解题思路

由于最近公共祖先节点可以为节点本身,可以先判断两个节点中是否有一个是另一个的祖先节点,如果不是,则从根节点开始,判断两个节点是否在当前节点的同一侧,在,则将当前节点当作根结点,再次判断,直到两个节点一个大于当前节点一个小于当前节点,则当前节点为两个节点的公共祖先节点。

解题方法

俺这版

【LeetCode】11. Lowest Common Ancestor of a Binary Search Tree· 二叉搜索树的最近公共祖先

/**
 * 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 (null == root || root == p || root == q) {
            return root;
        }
        if (null == p) {
            return q;
        }
        if (null == q) {
            return q;
        }
        TreeNode temp = root;
        while (null != temp) {
            if (p.val < temp.val) {
                if (q.val < temp.val) {
                    temp = temp.left;
                } else {
                    return temp;
                }
            } else if(p.val > temp.val){
                if (q.val > temp.val) {
                    temp = temp.right;
                } else {
                    return temp;
                }
            }else{
                return temp;
            }
        }
        return null;
    }
}

官方版

方法1:两次遍历

【LeetCode】11. Lowest Common Ancestor of a Binary Search Tree· 二叉搜索树的最近公共祖先

class Solution {
    public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
        List<TreeNode> path_p = getPath(root, p);
        List<TreeNode> path_q = getPath(root, q);
        TreeNode ancestor = null;
        for (int i = 0; i < path_p.size() && i < path_q.size(); ++i) {
            if (path_p.get(i) == path_q.get(i)) {
                ancestor = path_p.get(i);
            } else {
                break;
            }
        }
        return ancestor;
    }

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

方法2:一次遍历

【LeetCode】11. Lowest Common Ancestor of a Binary Search Tree· 二叉搜索树的最近公共祖先

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.left;
            } else if (p.val > ancestor.val && q.val > ancestor.val) {
                ancestor = ancestor.right;
            } else {
                break;
            }
        }
        return ancestor;
    }
}

总结

记得要借助二叉搜索树的特性~~

二叉搜索树(Binary Search Tree)又称二叉排序树,具有以下性质:

  • 若它的左子树不为空,则左子树上所有节点的值都小于根节点的值

  • 若它的右子树不为空,则右子树上所有节点的值都大于根节点的值

  • 它的左右子树也分别为二叉搜索树

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/135416.html

(0)
飞熊的头像飞熊bm

相关推荐

发表回复

登录后才能评论
极客之音——专业性很强的中文编程技术网站,欢迎收藏到浏览器,订阅我们!