题目描述:

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. 外观模式Facade pattern

    http://www.runoob.com/design-pattern/facade-pattern.html 外观模式 外观模式(Facade Pattern)隐藏系统的复杂性,并向客户端提供了一 ...

  2. 对象扩展运算符(…)与rest运算符

    对象扩展运算符(…) 当编写一个方法时,我们允许它传入的参数是不确定的.这时候可以使用对象扩展运算符来作参数,看一个简单的列子: function xzdemo(...arg){ console.lo ...

  3. jlink的SWD与JTAG下载模式的对应接线方法

     参考博客:http://blog.csdn.net/qq_26093511/article/details/59484249 (1)如果用jtag模式下载的话需要接线:          jlink ...

  4. 【译】第41节---EF6-事务

    原文:http://www.entityframeworktutorial.net/entityframework6/transaction-in-entity-framework.aspx EF默认 ...

  5. Java单例设计模式(实现Java的一个类只有一个对象)

    单例设计模式的定义:单例设计模式是一种软件设计模式,在它的核心包含一个称为单例类的核心类. 核心便是希望一个类只有一个对象.  如何实现类在内存中只有一个对象呢? 第一步:构造私有:第二步:本身提供一 ...

  6. 【转】myeclipse 自定义视图Customize Perspective 没有反应

    官网查了下,解释如下:   附上链接https://www.myeclipseide.com/PNphpBB2-viewtopic-t-30151.html,大概意思是按如下图所示步骤更新即可.读者可 ...

  7. jquery事件重复绑定的几种解决方法 (二)

    防止事件重复绑定共有4种方法: bind().unbind()方法 live().die()方法 off().on()方法 one()方法 一.bind().unbind()方法 bind();绑定事 ...

  8. C#支付宝多次回调问题

    //必须删除掉页面上的默认内容,不然支付宝会多次回调 <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml&q ...

  9. 【Web Service】

    Restful: (Representational State Transfer  表现层[指客户端]状态[指服务器端]转化) RPC: RPC 风格的开发关注于服务器/客户端之间的方法调用, 而并 ...

  10. VC.【转】采用_beginthread/_beginthreadex函数创建多线程

    https://blog.csdn.net/cbnotes/article/details/8331632 还可以看这个网址的内容:[多线程]VC6使用_beginthread开启多线程的方法-技术宅 ...