关于LeetCode中Lowest Common Ancestor of a Binary Search Tree一题的理解
来源:互联网 发布:淘宝直通车开车后流程 编辑:程序博客网 时间:2024/06/03 16:19
题目如下:
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 v and w as the lowest node in T that has both v and w as descendants (where we allow a node to be a descendant of itself).”
_______6______ / \ ___2__ ___8__ / \ / \ 0 _4 7 9 / \ 3 5
For example, the lowest common ancestor (LCA) of nodes2
and8
is 6
. Another example is LCA of nodes2
and4
is 2
, since a node can be a descendant of itself according to the LCA definition.
public class Solution { static boolean isFind=false; public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) { if(root == null){ return null; } root.left = lowestCommonAncestor(root.left, p, q); root.right = lowestCommonAncestor(root.right, p ,q); if(((root.val == p.val || root.val == q.val) && root.right!=null && (root.right.val == p.val || root.right.val == q.val)) || ((root.val == p.val || root.val == q.val) && root.left!=null && (root.left.val == p.val || root.left.val == q.val)) || (root.left!=null && root.right!=null &&(root.right.val == p.val || root.right.val == q.val) && (root.left.val == p.val || root.left.val == q.val))){ isFind = true; return root; }else if(root.right!=null && (root.right.val == p.val || root.right.val == q.val)){ return root.right; }else if(root.left!=null && (root.left.val == p.val || root.left.val == q.val)){ return root.left; }else if(root.val == p.val || root.val == q.val){ return root; }else if(isFind){ return root.left==null?root.right:root.left; }else{ return null; } }}感觉还是超吓人的,我感觉主要原因还是没有利用上“二叉搜索树”这个条件,那我们还是来看看评论区的大神们都是如何实现的吧!来来来客官,X大的来了,这个解决方案啊,看得我都excited了!快快快,直接上代码:
public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) { for(; ;) { if((p.val - root.val) * (q.val - root.val) <= 0)return root; if(p.val < root.val) root = root.left; else root = root.right; } }最关键的代码就是第二行的代码,如果给定节点p和q的val值一个比当前节点大(或等于),另一个比当前节点小(或等于),说明当前节点肯定是需要返回的目标节点,仔细想一想真的是这样!比我的代码不知道高到哪里去了!还有一种是它的递归形式,如下所示:
public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) { if(root.val > p.val && root.val > q.val){ return lowestCommonAncestor(root.left, p, q); }else if(root.val < p.val && root.val < q.val){ return lowestCommonAncestor(root.right, p, q); }else{ return root; } }如果你还想更简单点,下面这个只有三行,思路一样:
public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) { while ((root.val - p.val) * (root.val - q.val) > 0) root = p.val < root.val ? root.left : root.right; return root;}大致就是这样了,我记得昨天我还说过要好好看题,今天直接实力打脸,没事我的脸一点都不疼,就是肿了......果然套路和岳老师说的一样,"我说的字啊,每一个都有用!"。
0 0
- 关于LeetCode中Lowest Common Ancestor of a Binary Search Tree一题的理解
- 【leetcode每日一题】235.Lowest Common Ancestor of a Binary Search Tree
- leetcode 每日一题 235. Lowest Common Ancestor of a Binary Search Tree
- leetcode 235: Lowest Common Ancestor of a Binary Search Tree
- LeetCode 235. Lowest Common Ancestor of a Binary Search Tree
- leetcode: Lowest Common Ancestor of a Binary Search Tree
- [leetcode][dfs] Lowest Common Ancestor of a Binary Search Tree
- [leetcode] 235.Lowest Common Ancestor of a Binary Search Tree
- 【LeetCode】235 Lowest Common Ancestor of a Binary Search Tree
- [LeetCode]235.Lowest Common Ancestor of a Binary Search Tree
- leetcode 235 Lowest Common Ancestor of a Binary Search Tree
- [leetcode] Lowest Common Ancestor of a Binary Search Tree
- LeetCode Lowest Common Ancestor of a Binary Search Tree
- leetCode(38):Lowest Common Ancestor of a Binary Search Tree
- leetcode-235-Lowest Common Ancestor of a Binary Search Tree
- [LeetCode]Lowest Common Ancestor of a Binary Search Tree
- Leetcode: Lowest Common Ancestor of a Binary Search Tree
- leetcode[235]:Lowest Common Ancestor of a Binary Search Tree
- CUDA学习--页锁定主机内存
- bzoj 2820-莫比乌斯函数反演+分块优化 +换元优化
- 方欣科技算法面试:蛇形矩阵2
- 查找和替换文本和其他 Word 文档中的数据(转)
- srs compile error
- 关于LeetCode中Lowest Common Ancestor of a Binary Search Tree一题的理解
- 【手机硬件】【硬件】手机硬件组成
- 自定义控件(一)
- lightoj 1037 - Agent 47
- 每日小记(16.9.6)
- 常见算法及问题场景——字符串匹配
- Android官方开发文档Training系列课程中文版:布局性能优化之布局层级优化
- 故事
- Peer-to-Peer (P2P) communication across middleboxes(翻译3)