题目描述:

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 递归与非递归)的更多相关文章

  1. [geeksforgeeks] Lowest Common Ancestor in a Binary Search Tree.

    http://www.geeksforgeeks.org/lowest-common-ancestor-in-a-binary-search-tree/ Lowest Common Ancestor ...

  2. 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 ...

  3. 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 ...

  4. 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 利用二 ...

  5. 【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 ...

  6. 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 ...

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

  8. [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 ...

  9. [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 ...

  10. [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 ...

随机推荐

  1. FJUT seventh的tired树上路径(01字典树)题解

    思路(来自题解): 众所周知树上两个点xy的距离是deep[x]+deep[y]-deep[lca(x,y)]*2 然后我们把这个加减法换成异或,我们就会发现,deep[lca(x,y)]被消掉了 所 ...

  2. 【Finchley】【新特性】Spring Cloud Finchley 新特性

    Finchley 正式版的发布貌似经历了相当长的时间,这次的重大发布主要带来了以下 4 项重大更新. 重大更新 1.新增 Spring Cloud Gateway 组件 Spring Cloud Ga ...

  3. 记一次oracle创建一个新数据库,并导入正式环境数据库备份的dmp包过程

    背景:正式环境oracle数据库定时用exp备份一个dmp包,现在打算在一台机器上创建一个新数据库,并导入这个dmp包. 1.创建数据库 开始 -> 所有程序 -> Oracle -> ...

  4. 如何用conda安装软件|处理conda安装工具的动态库问题

    conda的确是一个非常好的工具,对于初学者而言,安装软件就跟用XXX软件管理器一样方便.正因为他如此便利,以至于我介绍如何手动安装工具时,总有人问我为啥不用conda. 我用conda,并且用的很好 ...

  5. 【三十二】thinkphp之连接数据库、实例化模型

    1.连接数据库 Thinlphp内置了抽象数据库访问层,把不同的数据操作封装起来.我们只需要调用公共的DB类进行操作即可.DB类会自动调用相应的数据库驱动来处理. 在应用目录/common/conf/ ...

  6. P1182 数列分段`Section II`

    传送门 思路: 求数列每段和的最大值的最小值,很明显是用二分法求解,加贪心检验.本题关键是要怎么去高效的check,可以考虑一个贪心的思路,能加的就加上,不能则新开一段,so对于二分的值 u ,我们从 ...

  7. docker 日志分析

    日志分两类,一类是 Docker 引擎日志:另一类是 容器日志. Docker 引擎日志 Docker 引擎日志 一般是交给了 Upstart(Ubuntu 14.04) 或者 systemd (Ce ...

  8. html 进度条

    <html> <head> <title>进度条</title> <style type="text/css"> .co ...

  9. idea输出目录详解

    引言:在项目中遇到了一个问题,在使用idea时,项目中Tomcat的虚拟目录映射总是失败,而当我采用myeclipse时却能映射过去. 自己花费了很长时间,终于找出了问题所在,原来是由于idea自己采 ...

  10. Mybatis的SqlSession理解(一)

    SqlSession是Mybatis最重要的构建之一,可以认为Mybatis一系列的配置目的是生成类似JDBC生成的Connection对象的statement对象,这样才能与数据库开启“沟通”,通过 ...