2022-03-27 20:38:29 +08:00
< p > 给定一个二叉搜索树, 找到该树中两个指定节点的最近公共祖先。< / p >
2025-01-09 20:29:41 +08:00
< p > < a href = "https://baike.baidu.com/item/%E6%9C%80%E8%BF%91%E5%85%AC%E5%85%B1%E7%A5%96%E5%85%88/8918834?fr=aladdin" target = "_blank" > 百度百科< / a > 中最近公共祖先的定义为:“对于有根树 T 的两个结点 p、q, 最近公共祖先表示为一个结点 x, 满足 x 是 p、q 的祖先且 x 的深度尽可能大(< strong > 一个节点也可以是它自己的祖先< / strong > )。”< / p >
2022-03-27 20:38:29 +08:00
< p > 例如,给定如下二叉搜索树: root = [6,2,8,0,4,7,9,null,null,3,5]< / p >
2025-01-09 20:29:41 +08:00
< p > < img alt = "" src = "https://assets.leetcode-cn.com/aliyun-lc-upload/uploads/2018/12/14/binarysearchtree_improved.png" / > < / p >
2022-03-27 20:38:29 +08:00
< p > < / p >
2025-01-09 20:29:41 +08:00
< p > < strong > 示例 1: < / strong > < / p >
2022-03-27 20:38:29 +08:00
2025-01-09 20:29:41 +08:00
< pre >
< strong > 输入:< / strong > root = [6,2,8,0,4,7,9,null,null,3,5], p = 2, q = 8
< strong > 输出:< / strong > 6
< strong > 解释:< / strong > 节点 < code > 2 < / code > 和节点 < code > 8 < / code > 的最近公共祖先是 < code > 6。< / code >
2022-03-27 20:38:29 +08:00
< / pre >
2025-01-09 20:29:41 +08:00
< p > < strong > 示例 2: < / strong > < / p >
2022-03-27 20:38:29 +08:00
2025-01-09 20:29:41 +08:00
< pre >
< strong > 输入:< / strong > root = [6,2,8,0,4,7,9,null,null,3,5], p = 2, q = 4
< strong > 输出:< / strong > 2
< strong > 解释:< / strong > 节点 < code > 2< / code > 和节点 < code > 4< / code > 的最近公共祖先是 < code > 2< / code > , 因为根据定义最近公共祖先节点可以为节点本身。< / pre >
2022-03-27 20:38:29 +08:00
< p > < / p >
2025-01-09 20:29:41 +08:00
< p > < strong > 说明:< / strong > < / p >
2022-03-27 20:38:29 +08:00
< ul >
< li > 所有节点的值都是唯一的。< / li >
< li > p、q 为不同节点且均存在于给定的二叉搜索树中。< / li >
< / ul >
< p > 注意:本题与主站 235 题相同:< a href = "https://leetcode-cn.com/problems/lowest-common-ancestor-of-a-binary-search-tree/" > https://leetcode-cn.com/problems/lowest-common-ancestor-of-a-binary-search-tree/< / a > < / p >