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. 设置阿里云maven中央仓库的settings.xml

    本来想找一个可用的设置文件,结果乱七八糟的,干脆自己做了一个,同时还放上了Spring的SNAPSHOT和MILESTONE/RELEASE仓库,希望能帮到一些人. <?xml version= ...

  2. js抽奖

    http://www.aichengxu.com/view/64369 <!Doctype html><html><head><meta http-equiv ...

  3. win2008server系统下文件替换权限

    因为那里的文件默认只有系统才有修改权限.选中要替换的文件(一次只能选一个),属性->安全->高级->所有者(选更改)->高级->立即查找->选择 Everyone, ...

  4. 如何进行正确的SQL性能优化

    在SQL查询中,为了提高查询的效率,我们常常采取一些措施对查询语句进行SQL性能优化.本文我们总结了一些优化措施,接下来我们就一一介绍. 1.查询的模糊匹配 尽量避免在一个复杂查询里面使用 LIKE ...

  5. php 中文正则

    utf8编码中文 preg_match("/^[\x{4e00}-\x{9fa5}]+$/u") 而不是 "/^[\x4e00-\x9fa5]+$/u"

  6. Android Studio安装genymotion模拟器

    1.Genymotion的安装: Genymotion无疑是目前最快最好用的模拟器.官网下载地址:https://www.genymotion.com/ 先注册,然后下载,安装VirtualBox最简 ...

  7. win7连接共享打印机 错误为

    1.  xp局域网共享打印机,本机运行 –>  \\共享电脑ip –> 选择驱动目录 .INF 文件 –> 确认. 直接报错..... 2.  参考方法: 开始——设备和打印机——添 ...

  8. OkHttp使用介绍

    版权声明: 欢迎转载,但请保留文章原始出处 作者:GavinCT 出处:http://www.cnblogs.com/ct2011/p/4001708.html 为什么需要一个HTTP库 Androi ...

  9. [转]编译VC++程序warning C4819快速解决

    文章来自:http://www.cppblog.com/API/archive/2012/11/12/195056.aspx 编译VC++程序的时候出现如下提示警告: warning C4819: T ...

  10. JS更随机的随机数

    一.问题背景 一个二维平面上有一群NPC,每一回合可以随机向上/下/左/右任一方向走1步,有单位碰撞体积(NPC位置不能重合) 规则就这么简单,初始情况下这群NPC是被人工均匀分布在二维平面上的,运行 ...