Reference:

http://blog.csdn.net/v_july_v/article/details/18312089 

http://leetcode.com/2011/07/lowest-common-ancestor-of-a-binary-tree-part-ii.html

(1) Is the tree a BST or not?

BST的话,我们就能按照BST的定义思考了。当前遍历的node如果比我们要找的两个点都大,说明那两个点都在当前node的左边,所以这两个node的祖先肯定在当前node的左边,所以往左边找。反之,往右边找。如果这两个点一个大于当前node一个小于当前node,说明当前node就是LCA。 如果这两个点一个是另一个的祖先,那么这个点就是LCA。

代码如下:

                                                                            }

(2)那如果不是BST呢?一般Binary Tree.

a. Tree结构中没有parent域。

递归找。

代码如下:

         
                                                                                             }

b. 如果有parent域。

转化为两个linkedlist的交点问题。

代码如下:

              a = a + b;
         b = a - b;
         a = a - b;
     }
     
     Node getLCA(Node p, Node q){
                           
                       swap(h1, h2);
             swap(p, q);
         }
         
                  
                      q = q.parent;
         
                                                     p = p.parent;
             q = q.parent;
         }
         
              }

Lowest Common Ancestor of Two Nodes in a Binary Tree的更多相关文章

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

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

  3. 数据结构与算法(1)支线任务4——Lowest Common Ancestor of a Binary Tree

    题目如下:https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-tree/ Given a binary tree, fin ...

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

  5. Lowest Common Ancestor of a Binary Tree

    Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree. According ...

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

  7. leetcode 236. Lowest Common Ancestor of a Binary Tree

    Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree. According ...

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

  9. LeetCode: Lowest Common Ancestor of a Binary Search Tree 解题报告

    https://leetcode.com/submissions/detail/32662938/ Given a binary search tree (BST), find the lowest ...

随机推荐

  1. UICollectionView进阶练习

    上一篇中的干货看完,不觉感觉还是有点虚,今天我们来点实的,做了两个小DEMO,源码已放GitHub,主要是针对UICollectionView做了联系.第一个DEMO是针对UICollectionVi ...

  2. c#开发Mongo笔记第四篇

    今天是个伟大的日子,不得不说小苹果的歌词真是深入人心啊. 不过今天伟大并不是因为我种下一颗种子,而是我从今天不再写demo,而是进入项目的正式开发当中,毕竟项目时间有限(想必各位码农也都深有体会吧), ...

  3. ORA-01653:表空间扩展失败的问题以及增加表空间

    一.脚本修改方式: ----查询表空间使用情况---使用DBA权限登陆SELECT UPPER(F.TABLESPACE_NAME) "表空间名",D.TOT_GROOTTE_MB ...

  4. WebService 基本操作

    1.新建asp.net web 应用程序 2.添加web 服务webservice.asmx public string HelloWorld(int a) { if (a==1) { return ...

  5. Java复数的四则运算

    import java.util.Scanner;   import com.sun.jndi.url.iiopname.iiopnameURLContextFactory;   public cla ...

  6. js-BOM之offset家族、移动函数的封装升级(轮播图)

    Obj.style.width/obj.style.height与obj.offsetWidth/obj.offsetHeight的区别: <style> #div1{ height: 2 ...

  7. JavaScript-创建新数组

    <!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...

  8. 从nib文件里加载collectionViewCell

    如何取出在xib文件里绘制的collectionViewCell ? 1.获得nib文件 UINib *nib = [[UINib NibWithName:@"xib文件的名字"] ...

  9. Python字符转换

    Python提供了ord和chr两个内置的函数,用于字符与ASCII码之间的转换. 如:>>> print ord('a') 97 >>> print chr(97 ...

  10. java后台开发传输乱码&&接口post传参失败

    起因: 前几天遇到的问题,才有时间记录,需求:本地生成xml形式的字符串以参数形式用post方法传送到对方的固定接口: 这个需求写的时候感觉很容易,本地测试的时候,也觉得很简单就过了,然后和对方联调的 ...