[LeetCode] Lowest Common Ancestor of a Binary Search Tree 二叉搜索树的最小共同父节点

时间:2022-03-28 06:14:13

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).”

Given binary search tree:  root = [6,2,8,0,4,7,9,null,null,3,5]

[LeetCode] Lowest Common Ancestor of a Binary Search Tree 二叉搜索树的最小共同父节点

Example 1:

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

Example 2:

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

Note:

  • All of the nodes' values will be unique.
  • p and q are different and both values will exist in the BST.

这道题让我们求二叉搜索树的最小共同父节点, LeetCode中关于BST的题有 Validate Binary Search Tree, Recover Binary Search Tree, Binary Search Tree Iterator, Unique Binary Search Trees, Unique Binary Search Trees IIConvert Sorted Array to Binary Search Tree , Convert Sorted List to Binary Search Tree 和 Kth Smallest Element in a BST。这道题我们可以用递归来求解,我们首先来看题目中给的例子,由于二叉搜索树的特点是左<根<右,所以根节点的值一直都是中间值,大于左子树的所有节点值,小于右子树的所有节点值,那么我们可以做如下的判断,如果根节点的值大于p和q之间的较大值,说明p和q都在左子树中,那么此时我们就进入根节点的左子节点继续递归,如果根节点小于p和q之间的较小值,说明p和q都在右子树中,那么此时我们就进入根节点的右子节点继续递归,如果都不是,则说明当前根节点就是最小共同父节点,直接返回即可,参见代码如下:

解法一:

class Solution {
public:
TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {
if (!root) return NULL;
if (root->val > max(p->val, q->val))
return lowestCommonAncestor(root->left, p, q);
else if (root->val < min(p->val, q->val))
return lowestCommonAncestor(root->right, p, q);
else return root;
}
};

当然,此题也有非递归的写法,用个 while 循环来代替递归调用即可,然后不停的更新当前的根节点,也能实现同样的效果,代码如下:

解法二:

class Solution {
public:
TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {
while (true) {
if (root->val > max(p->val, q->val)) root = root->left;
else if (root->val < min(p->val, q->val)) root = root->right;
else break;
}
return root;
}
};

Github 同步地址:

https://github.com/grandyang/leetcode/issues/235

类似题目:

Lowest Common Ancestor of a Binary Tree

参考资料:

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

https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-search-tree/discuss/64980/C%2B%2B-Recursive-and-Iterative

https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-search-tree/discuss/64963/3-lines-with-O(1)-space-1-Liners-Alternatives

LeetCode All in One 题目讲解汇总(持续更新中...)

[LeetCode] Lowest Common Ancestor of a Binary Search Tree 二叉搜索树的最小共同父节点的更多相关文章

  1. &lbrack;LeetCode&rsqb; 235&period; Lowest Common Ancestor of a Binary Search Tree 二叉搜索树的最小共同父节点

    Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the BS ...

  2. &lbrack;LeetCode&rsqb; 235&period; Lowest Common Ancestor of a Binary Search Tree 二叉搜索树的最近公共祖先

    Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the BS ...

  3. 235 Lowest Common Ancestor of a Binary Search Tree 二叉搜索树的最近公共祖先

    给定一棵二叉搜索树, 找到该树中两个指定节点的最近公共祖先. 详见:https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-s ...

  4. LeetCode&colon; Lowest Common Ancestor of a Binary Search Tree 解题报告

    https://leetcode.com/submissions/detail/32662938/ Given a binary search tree (BST), find the lowest ...

  5. &lbrack;LeetCode&rsqb;Lowest Common Ancestor of a Binary Search Tree

    Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the BS ...

  6. LeetCode——Lowest Common Ancestor of a Binary Search Tree

    Description: Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given no ...

  7. Python3解leetcode Lowest Common Ancestor of a Binary Search Tree

    问题描述: Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in ...

  8. LeetCode Lowest Common Ancestor of a Binary Search Tree (LCA最近公共祖先)

    题意: 给一棵二叉排序树,找p和q的LCA. 思路: 给的是BST(无相同节点),那么每个节点肯定大于左子树中的最大,小于右子树种的最小.根据这个特性,找LCA就简单多了. 分三种情况: (1)p和q ...

  9. leetcode面试准备&colon;Lowest Common Ancestor of a Binary Search Tree &amp&semi; Binary Tree

    leetcode面试准备:Lowest Common Ancestor of a Binary Search Tree & Binary Tree 1 题目 Binary Search Tre ...

随机推荐

  1. 自制Azure中国版&OpenCurlyDoubleQuote;加血包”

    Micrsoft Azure中国版的国际出口最近升级为电话线拨号模式,目测为10个用户共享一条56kb的电话线拨号链路.有图有真相: 中国的IT从业者,有三分之一的职业生涯时间是在跟网络斗智斗勇.这点 ...

  2. 适配布局-ios

    // 系统的约束代码 @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; UIView *superVi ...

  3. CleanBlog&lpar;个人博客&plus;源码&rpar;

    CleanBlog是一个高端(低调).大气(简洁)的个人博客系统,之前在网上看到了好多个人博客网站,感觉很酷的,自己也想搭建一个,最近 刚学完SSM(Spring/SpringMVC/MyBatis) ...

  4. Yii2 – 如何写一个插件 , 如何做一个扩展

    原文地址: http://www.fancyecommerce.com/2016/05/10/yii2-%E5%A6%82%E4%BD%95%E5%86%99%E4%B8%80%E4%B8%AA%E6 ...

  5. Linux防火墙规则的查看、添加、删除和修改

    这里只列出比较常用的参数,详细的请查看man iptables 1.查看 iptables -nvL –line-number -L查看当前表的所有规则,默认查看的是filter表,如果要查看NAT表 ...

  6. CSS 奇技淫巧十八招

    http://www.tuicool.com/articles/VZneI3   開始覺得自己會寫 CSS 也算有一段時間了,常常遇到一些非常實用的技巧不斷地反覆使用,但是我個人覺得對初學者來說很難從 ...

  7. 简化对象extend拓展

    发现对对象继承或拷贝的时候,总是要$点来点去好麻烦,我的解决办法如下: (function(){ Object.prototype.extend = function(o){ $.extend(tru ...

  8. MongoDB 备份方法

    翻译自 http://docs.mongodb.org/manual/core/backups/ 有以下几种方法来备份MongoDB群集: 通过复制底层数据文件来备份 通过mongodump来备份 通 ...

  9. Linear Regression&lpar;线性回归&rpar;(二)—正规方程(normal equations)

    (整理自AndrewNG的课件,转载请注明.整理者:华科小涛@http://www.cnblogs.com/hust-ghtao/) 在上篇博客中,我们提出了线性回归的概念,给出了一种使代价函数最小的 ...

  10. &lbrack;PGM&rsqb; Exact Inference for calculating marginal distribution

    如何在贝叶斯网络中求解某变量的边缘分布? 这是一个问题. 贝叶斯网络如下: CPTs如下: (1) How to compute p( L | C = high )? p( L | C = high  ...