LeetCode------Lowest Common Ancestor of a Binary Search Tree
来源:互联网 发布:公需科目大数据网站 编辑:程序博客网 时间:2024/06/05 02:10
题目简介
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 nodes 2
and 8
is 6
. Another example is LCA of nodes 2
and 4
is 2
, since a node can be a descendant of itself according to the LCA definition.
给定一个二进制搜索树(BST),找到最近公共祖先(LCA)的两个给定节点的成就。
根据维基百科的定义:“最近公共祖先是定义两节点v和w之间最低的节点不具有V和W的后裔(我们允许一个节点有自己的后裔)。”
例如,最近的公共祖先(LCA)节点2和8是6。另一个例子是LCA节点2和4是2,因为一个节点可以是自己根据LCA的定义。
自己的解法
public class Solution { public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) { if(p.val == root.val||q.val ==root.val) return root; while(true){ TreeNode leftroot =findCurrent(root, p); TreeNode rightroot =findCurrent(root, q); if(leftroot != null&&leftroot.val == q.val) return leftroot; else if(rightroot !=null&&rightroot.val == p.val) return rightroot; else if(leftroot != null&&rightroot != null&&leftroot.val == rightroot.val) return leftroot; p = leftroot; q = rightroot; } } public TreeNode findCurrent(TreeNode root, TreeNode p){ if(p.val == root.val) return root; if(root == null) return root; else{ TreeNode left = root.left; TreeNode right = root.right; if(left != null&&left.val == p.val) return root; else if(right !=null&&right.val == p.val) return root; else if(p.val <root.val) return findCurrent(left,p); else return findCurrent(right,p); } }}
题目的意思是寻找一棵二叉排序数的两个节点的最近祖先,这个最近祖先可以包含它自己。我的思路是这样的通过findCurrent函数找到任意一个节点的祖先节点,然后再主函数里判断,如果没找到的话,继续寻找祖先节点的祖先节点。我提交一直有问题,原因是这样的,比如这样一棵二叉排序树[5,3,6,2,4,null,null,1],给的节点是1和3,按照我的算法返回的是5而正确的节点是3。我的算法如果要寻找的节点某一棵刚好是另一棵的祖先节点,就无法返回正确答案。一般都会返回根节点。
Hot解法
public class Solution { 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 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
- Lowest Common Ancestor of a Binary Search Tree-leetcode
- 【leetcode】Lowest Common Ancestor of a Binary Search Tree
- 【Leetcode】Lowest Common Ancestor of a Binary Search Tree
- leetcode 104
- Date和Calendar时间操作常用方法及示例
- 中小学教育问题
- 安装NTFS For Mac时显示文件已损坏怎么办
- InterlliJ Debug方式启动:Method breakpoints may dramatically slow down debugging
- LeetCode------Lowest Common Ancestor of a Binary Search Tree
- leetcode 8: String to Integer (myAtoi,C库函数atoi模拟) (剑指offer 面试题49) 解题报告
- mysql 赋权笔记
- 【代码笔记】iOS-判断是否是iPhone5
- SDL使用内存像素数据绘制(SDL_CreateRGBSurfaceFrom)
- nrf52832 --- 资料下载
- Android开发中,那些让你相见恨晚的方法、类或接口
- leetcode 226
- iOS开发数据库篇—FMDB数据库队列