[LeetCode] 235. 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 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]
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 nodes2
and8
is6
.
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 nodes2
and4
is2
, 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 II,Convert 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/
LeetCode All in One 题目讲解汇总(持续更新中...)
[LeetCode] 235. Lowest Common Ancestor of a Binary Search Tree 二叉搜索树的最小共同父节点的更多相关文章
- [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 the BS ...
- [LeetCode] 235. 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 ...
- 235 Lowest Common Ancestor of a Binary Search Tree 二叉搜索树的最近公共祖先
给定一棵二叉搜索树, 找到该树中两个指定节点的最近公共祖先. 详见:https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-s ...
- leetcode 235. Lowest Common Ancestor of a Binary Search Tree 236. Lowest Common Ancestor of a Binary Tree
https://www.cnblogs.com/grandyang/p/4641968.html http://www.cnblogs.com/grandyang/p/4640572.html 利用二 ...
- LeetCode 235. 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 ...
- leetcode 235. 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 ...
- (easy)LeetCode 235.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 ...
- Java [Leetcode 235]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 ...
- Leetcode 235 Lowest Common Ancestor of a Binary Search Tree 二叉树
给定一个二叉搜索树的两个节点,找出他们的最近公共祖先,如, _______6______ / \ ___2__ ___8__ / \ / \ 0 4 7 9 / \ 3 5 2和8的最近公共祖先是6, ...
随机推荐
- 【shell脚本】创建账户及删除账户,批量创建账户及批量删除账户===autoCreateUser.sh
一.字符串运算符 二.创建账户 1.提示用户输入用户名和密码,脚本自动创建相应的账户及配置密码.如果用户不输入账户名,则提示必须输入账户名并退出脚本;如果用户不输入密码,则统一使用默认的 123456 ...
- java文件操作File类
1.文件路径操作 测试方法 @Test public void test5() { StringBuffer succBuffer = new StringBuffer("D:\\home\ ...
- iOS 一个项目添加多个TARGET
项目开发中会存在测试.正式等不同环境,需对应不同接口Host地址.项目名称等等配置.如果每次只有一个项目target的话每次打包的时候替换会很麻烦,而且容易出错.所以我们可以通过创建多个不同配置的ta ...
- Python复杂对象转JSON
Python复杂对象转JSON在Python对于简单的对象转json还是比较简单的,如下: import json d = {'a': 'aaa', 'b': ['b1', 'b2', 'b3'], ...
- nginx的6种负载均衡策略
在服务器集群中,Nginx起到一个反向代理服务器的作用.为了避免单独一个服务器压力过大导致服务器奔溃,就需要将不同用户的请求转发给不同给不同的服务器,保证集群中的每一台服务器都能正常运作,这种机制就叫 ...
- Window权限维持(八):时间服务器
Windows操作系统正在利用时间提供者体系结构,以便从网络中的其他网络设备或客户端获取准确的时间戳.时间提供者以DLL文件的形式实现,该文件位于System32文件夹中.Windows启动期间将启动 ...
- vue入门案例
1.技术在迭代,有时候你为了生活没有办法,必须掌握一些新的技术,可能你不会或者没有时间造轮子,那么就先把利用轮子吧. <!DOCTYPE html> <html> <he ...
- C# 实现 奇数偶数排序,奇数在前,偶数在后
public static void SortByOddAndEven(int []arr) { for (int i = 0; i < arr.Length; i++) { for (int ...
- Linux下java验证码不显示:Could not initialize class sun.awt.X11FontManager
一.问题 javaweb项目,登录的时候有个图片验证码的功能.在Windows本地测试能够正常显示,部署到Linux上就不行了.报错如下: org.springframework.web.util.N ...
- win10笔记本电脑连wifi显示“无internet,安全”解决办法
吹一波, 不出意外的话,这应该是网上最全最详细的解决办法......毕竟妹子的电脑遇到了问题,咱一定要给她解决啊. 问题描述:连上了WiFi,显示“无Internet,安全”.但实际上她的电脑是有网的 ...