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. C# WebClient 实现上传下载网络资源

    下载数据 WebClient wc = new WebClient();1 string str= wc.DownloadString("地址")://直接下载字符串 2 wc.D ...

  2. 关于Plupload结合上传插件jquery.plupload.queue的使用

    之前使用过很多的上传组件,但对各种浏览器的兼容性太差,不得不放弃!! plupload 是款很强大的上传组件,不得不推荐.plupload 前端根据浏览器不同选择使用Html5. Gears, Sil ...

  3. Android(Xamarin)之旅(四)

    这么晚了,可能也因为一点事情,就说打开博客园看看,结果 http://www.cnblogs.com/mindwind/p/5125248.html 这篇文章瞬间吸引了我.暗暗的回想一下,十年之后,我 ...

  4. mybatis 控制台打印sql

    开发时调试使用 <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBe ...

  5. ICE系列之2——ICE的服务与好处

    ice服务: IcePack         我们在第 12 页提到过, IcePack 是 Ice 的定位服务,用于在使用间接绑定时把符号性的 (symbolic)适配器名解析为协议-地址对. 除了 ...

  6. linux 清理内存命令 查看内存命令

    查看内存: 我们可以用free命令查看内存信息: free -g total used free shared buffers cachedMem: 15   15    0     0       ...

  7. window 2003 配置FTP +防火墙设置

    2保险的做法是 不允许匿名登录,吧钩去掉 后面我们会添加一个用户,并且赋予权限 3 主目录 可以设置时当前计算机目录或者是另一台计算机目录(映射) FTP站点目录:浏览定位FTP文件所在站点,给予是否 ...

  8. UWP深入学习三:依赖属性、附加属性和数据绑定

    Dependency properties overview Custom dependency properties Attached properties overview Custom atta ...

  9. 如何评估ETL的数据加载时间

    简述如何评估大型ETL数据加载时间. 答:评估一个大型的ETL的数据加载时间是一件很复杂的事情.数据加载分为两类,一类是初次加载,另一类是增量加载. 在数据仓库正式投入使用时,需要进行一次初次加载,而 ...

  10. 关闭Ubuntu 12.04的内部错误提示

    刚装完系统后,才安装一个输入法重启电脑后,竟然就提示'内部错误'需要提交报告,什么状况? 发扬'不求甚解'的光荣传统,我又不搞Linux开发,对我来说只是个工具而已,工具出问题了解决问题即可不想劳神深 ...