题目描述:

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. CssClass="Hidden"和Visible="False"

    <asp:Label ID="lblNoCustomTableItemCheckedInfo" runat="server" CssClass=" ...

  2. Vue.extend构造器和$mount实例构造组件后可以用$destroy()进行卸载,$forceUpdate()进行更新,$nextTick()数据修改

    html <div id="app"> </div> <p><button onclick="destroy()"&g ...

  3. No mapping found for HTTP request with URI [/Portal/download] in DispatcherServlet with name 'springmvc'

    本文为博主原创,未经允许不得转载: 遇到这个异常,总结一下这个问题发生的原因: 这个原因是在springmvc中在DispatcherServlet分发请求时,解析不到相应的请求路径.后台要请求的路径 ...

  4. Python深入:Distutils发布Python模块--转载

    https://blog.csdn.net/gqtcgq/article/details/49255995 Distutils可以用来在Python环境中构建和安装额外的模块.新的模块可以是纯Pyth ...

  5. Servlet中web.xml的配置

    引言:这是一个采用原生Servlet开发的项目的一个简要配置,在这里记录一下,以便以后用到了 可以直接copy,如又侵权,请联系本博主. <?xml version="1.0" ...

  6. STL_map.插入

    环境:Win7x64.vs08x86 1.类中这样声明:map<string, list<string>> FmapTagAttr; 2.插入数据时这样: list<st ...

  7. gc调优我们到底在调整什么

    java开发一般都会涉及到jvm调优其中gc调优是个重点项.那gc调优调整的究竟是什么呢准确来说是业务.下面围绕这个话题展开 起因 为什么说是业务呢得从cc++开始说起如果说是用c/c++做开发运行的 ...

  8. python入门知识点(下)

    51.函数的文档注释及作用 """ 函数的文档注释: 函数的注释文字. 必须添加到函数定义的那一行的下面一行. 好处: 当使用Ctrl + Q查看函数的使用说明文档时,能 ...

  9. DAY1 计算机组成和操作系统

    一.编程与编程目的 1.编程语言的定义 编程语言是人与计算机之间沟通的介质 2.什么是编程 编程就是程序员通过编程语言让计算机实现所想做的事 3.编程的目的 解放人力,让计算机按照人的逻辑思维进行工作 ...

  10. JS添加/移除事件

    事件的传播方式 <div id="father"> <div id="son"></div> </div> &l ...