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

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).”

        _______3______
/ \
___5__ ___1__
/ \ / \
6 _2 0 8
/ \
7 4

For example, the lowest common ancestor (LCA) of nodes 5 and 1 is 3. Another example is LCA of nodes 5and 4 is 5, since a node can be a descendant of itself according to the LCA definition.

更新于20180513

若pq都在某个节点的左边,就到左子树中查找,如果都在右边 就到右子树种查找。

要是pq不在同一边,那就表示已经找到第一个公共祖先。

 class Solution {
public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
if(!cover(root,p)||!cover(root,q)) return null;
return LCAhelp(root,p,q);
}
private TreeNode LCAhelp(TreeNode root,TreeNode p,TreeNode q ){
if(root==null) return null;
if(root==p||root==q) return root;
boolean q_is_on_left = cover(root.left,q);
boolean p_is_on_left = cover(root.left,p); //分立两边
if(q_is_on_left!=p_is_on_left) return root; //在一边
else{
if(q_is_on_left)
return LCAhelp(root.left,p,q);
else
return LCAhelp(root.right,p,q);
}
}
private boolean cover(TreeNode root,TreeNode p){
//检查p是不是root的孙子 if(root==null) return false;
if(root==p) return true;
return cover(root.left,p)||cover(root.right,p);
}
}

解题思路

  • Divide & Conquer 的思路
  • 如果root为空,则返回空
  • 如果root等于其中某个node,则返回root
  • 如果上述两种情况都不满足,则divide,左右子树分别调用该方法
  • Divide & Conquer中这一步要考虑清楚,本题三种情况
  • 如果leftright都有结果返回,说明root是最小公共祖先
  • 如果只有left有返回值,说明left的返回值是最小公共祖先
  • 如果只有�right有返回值,说明�right的返回值是最小公共祖先
 class Solution {
public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
if(root ==null || p==root||q==root) return root;
TreeNode lp = lowestCommonAncestor(root.left,p,q);
TreeNode rp = lowestCommonAncestor(root.right,p,q);
if(lp!=null&&rp!=null) return root;
if(lp==null&&rp!=null) return rp;
if(lp!=null&&rp==null) return lp;
return null;
}
}

236. Lowest Common Ancestor of a Binary Tree(最低公共祖先,难理解)的更多相关文章

  1. 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 利用二 ...

  2. Leetcode之236. Lowest Common Ancestor of a Binary Tree Medium

    236. Lowest Common Ancestor of a Binary Tree Medium https://leetcode.com/problems/lowest-common-ance ...

  3. 【LeetCode】236. Lowest Common Ancestor of a Binary Tree

    Lowest Common Ancestor of a Binary Tree Given a binary tree, find the lowest common ancestor (LCA) o ...

  4. 【刷题-LeetCode】236. Lowest Common Ancestor of a Binary Tree

    Lowest Common Ancestor of a Binary Tree Given a binary tree, find the lowest common ancestor (LCA) o ...

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

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

  7. 【LeetCode】236. Lowest Common Ancestor of a Binary Tree 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  8. leetcode@ [236] Lowest Common Ancestor of a Binary Tree(Tree)

    https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-tree/ Given a binary tree, find the ...

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

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

随机推荐

  1. Xcode下开发c静态库for ios CPU架构 静态库合并

    新建一个Cocoa Touch Static  Library工程 1,先在工程左侧删除“工程名Tests”下的文件与文件夹(从内往外删,最后删除"工程名Tests文件夹") :D ...

  2. YUV转为RGB24及IplImage格式(I420和YV12)及Java版实现

    http://blog.csdn.net/xy365/article/details/18735849 ———————————————————————————————————————————————— ...

  3. [转]jna模拟指针开辟空间,数组首地址获取值

    http://blog.csdn.net/zhuzhichao0201/article/details/5817819 不是很明白,先记在这里 ———————————————————————————— ...

  4. android动画效果(转载)

    一.动画基本类型: 如下表所示,Android的动画由四种类型组成,即可在xml中定义,也可在代码中定义,如下所示: XML CODE 渐变透明度动画效果 alpha AlphaAnimation 渐 ...

  5. SQL宝典

    SQL Server 数据库的高级操作 (1) 批处理 (2) 变量 (3) 逻辑控制 (4) 函数 (5) 高级查询 */ (1)批处理 将多条SQL语句作为一个整体去编译,生成一个执行计划,然后, ...

  6. AWT从概念产生到完成实现只用了一个月

    这种糟糕的设计选择使得那些拥护Java“一次编写,到处运行 (write once, run anywhere)”信条的程序员们过得并不舒畅,因为AWT并不能保证他们的应用在各种平台上表现得有多相似. ...

  7. java io类图(转)

    转载:http://blog.csdn.net/fenglian521/article/details/1324010 Java IO 的类图 拿出来方便大家.Java IO表面上看起来比较乱,有了类 ...

  8. express搭建权限管理系统

    express搭建权限管理系统 权限管理,是管理系统中的常见组件.通常需要定义资源,把资源调配给用户,通过判断用户是否有权限增删改查来实现. 初衷: 使用express开发过的项目大大小小加在一起也有 ...

  9. 将socket5代理转换为http代理

    归根结底还是万恶的ZFW,MD弄得现在Google,gmail,dropbox都用不了了,看来这是要万民欢迎大中华局域网的到来了.最近在使用一同学的Shadowsocks来突破GFW的限制,可是电脑o ...

  10. 复合文档(Compound Document)读写栗子

    复合文件是把磁盘文件系统的管理方式移植到文件中---复合文件. 复合文档是由 Windows 系统通过 COM 提供的, 它能完成像 Windows 目录结构一样复杂的文件结构的存取:提示一下 Win ...