Lowest Common Ancestor of a Binary Search Tree(Java 递归与非递归)
题目描述:
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),寻找p和q的最小公共祖先结点
* 解题思路:首先找到到达p的路径和到达q的路径,将这两个路径存入队列中
* 然后对两个路径依次出队列,找出最后一组相同的结点,结束
* @param root
* @return
*/
public static TreeNode lowestCommonAncestor(TreeNode root,TreeNode p,TreeNode q){
if(root == null)
return null;
Queue<TreeNode> pPath = new LinkedList<TreeNode>();
Queue<TreeNode> qPath = new LinkedList<TreeNode>();
TreeNode pre = null;
//返回true,说明该结点存在;否则,说明给定的结点不存在,则无需再比较
if(findNode(root, p, pPath) && findNode(root, q, qPath)){
while(!pPath.isEmpty() && !qPath.isEmpty()){
TreeNode pNode = pPath.poll();
TreeNode qNode = qPath.poll();
if(pNode == qNode){ //记录最后一组相同结点
pre = pNode;
} else { //一旦不同,跳出循环
break;
}
}
}
return pre;
} /**
* 在二叉树中查找结点p
* 将其祖先结点依次存入队列
* 返回true,说明该结点存在;否则,说明给定的结点不存在,则无需再比较
* @param root
* @param p
* @param stack
*/
public static boolean findNode(TreeNode root,TreeNode p,Queue<TreeNode> queue){
if(root == null)
return false;
if(p.val == root.val){
queue.add(root);
return true;
}
else if(Integer.valueOf(p.val.toString()) < Integer.valueOf(root.val.toString())){ //在左子树查找
queue.add(root);
return findNode(root.left, p, queue);
}
else {//在右子树查找
queue.add(root);
return findNode(root.right, p, queue);
}
}
解法二:
/** 方法二
*
* 递归算法
* @param root
* @param p
* @param q
* @return
*/
public static TreeNode LCA(TreeNode root,TreeNode p,TreeNode q){
if (root == null || p == null || q == null) return null;
if (Integer.valueOf(p.val.toString()) < Integer.valueOf(root.val.toString()) && Integer.valueOf(q.val.toString()) < Integer.valueOf(root.val.toString())) //两个结点都在左子树
return LCA(root.left, p, q);
else if (Integer.valueOf(p.val.toString()) > Integer.valueOf(root.val.toString()) && Integer.valueOf(q.val.toString()) > Integer.valueOf(root.val.toString())) //两个结点都在右子树
return LCA(root.right, p, q);
else
return root; //若两个结点一个在左子树上,另一个在右子树上,则共同祖先结点为当前的root
}
Lowest Common Ancestor of a Binary Search Tree(Java 递归与非递归)的更多相关文章
- [geeksforgeeks] Lowest Common Ancestor in a Binary Search Tree.
http://www.geeksforgeeks.org/lowest-common-ancestor-in-a-binary-search-tree/ Lowest Common Ancestor ...
- leetcode面试准备:Lowest Common Ancestor of a Binary Search Tree & Binary Tree
leetcode面试准备:Lowest Common Ancestor of a Binary Search Tree & Binary Tree 1 题目 Binary Search Tre ...
- Lowest Common Ancestor of a Binary Search Tree、Lowest Common Ancestor of a Binary Search Tree
1.Lowest Common Ancestor of a Binary Search Tree Total Accepted: 42225 Total Submissions: 111243 Dif ...
- 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 (2 solutions)
Lowest Common Ancestor of a Binary Search Tree Given a binary search tree (BST), find the lowest com ...
- 235.236. Lowest Common Ancestor of a Binary (Search) Tree -- 最近公共祖先
235. Lowest Common Ancestor of a Binary Search Tree Given a binary search tree (BST), find the lowes ...
- LeetCode_235. Lowest Common Ancestor of a Binary Search Tree
235. Lowest Common Ancestor of a Binary Search Tree Easy Given a binary search tree (BST), find the ...
- [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 ...
- [CareerCup] 4.7 Lowest Common Ancestor of a Binary Search Tree 二叉树的最小共同父节点
4.7 Design an algorithm and write code to find the first common ancestor of two nodes in a binary tr ...
- [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 ...
随机推荐
- 【做题】BZOJ2342 双倍回文——马拉车&并查集
题意:有一个长度为\(n\)的字符串,求它最长的子串\(s\)满足\(s\)是长度为4的倍数的回文串,且它的前半部分和后半部分都是回文串. \(n \leq 5 \times 10^5\) 首先,显然 ...
- 编译 glibc-2.14 时出现的一个LD_LIBRARY_PATH不路径bug
../configure --prefix=/home/zzhy/wd/software/glibc-2.14 错误:checking LD_LIBRARY_PATH variable... cont ...
- 解决Geoserver请求跨域的几种思路,第二种思路用过
文章版权由作者李晓晖和博客园共有,若转载请于明显处标明出处:http://www.cnblogs.com/naaoveGIS/ 1.背景描述 跨域问题是浏览器同源安全制引起的特别常见的问题.不同前端语 ...
- 【Selenium2】【Shell】
E:\test_object>Python all_test.py >> report/log.txt 2>&1
- 基于 Python 和 Pandas 的数据分析(5) --- Concatenating and Appending
这一节我们将会介绍几种不同的合并数据的方法. 在我们这个不动产投资的例子中, 我们希望获取 51 个州的房产数据, 并把它们组合起来. 我们这样做有很多原因. 这样做既便于我们做分析, 同时也可以占用 ...
- ThreadPoolExecutor最佳实践--如何选择线程数
去年看过一篇<ThreadPoolExecutor详解>大致讲了ThreadPoolExecutor内部的代码实现. 总结一下,主要有以下四点: 当有任务提交的时候,会创建核心线程去执行任 ...
- 力扣(LeetCode)389. 找不同
给定两个字符串 s 和 t,它们只包含小写字母. 字符串 t 由字符串 s 随机重排,然后在随机位置添加一个字母. 请找出在 t 中被添加的字母. 示例: 输入: s = "abcd&quo ...
- 力扣(LeetCode)461. 汉明距离
两个整数之间的汉明距离指的是这两个数字对应二进制位不同的位置的数目. 给出两个整数 x 和 y,计算它们之间的汉明距离. 注意: 0 ≤ x, y < 231. 示例: 输入: x = 1, y ...
- vue生命周期钩子
转载自:https://segmentfault.com/a/1190000008010666?utm_source=tag-newest https://segmentfault.com/a/119 ...
- 在nodejs中的集成虹软人脸识别
==虹软官网地址==http://www.arcsoft.com.cn 在官网注册账号,并且申请人脸识别激活码, 选择SDK版本和运行系统(windows/linux/android/ios) ,我们 ...