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. ctrl +z

    #bg 1 [1]+ /root/bin/rsync.sh & 用 jobs 命令查看正在运行的任务: #jobs [1]+ Running /root/bin/rsync.sh & ...

  2. Using a long as ArrayList index in java

    http://stackoverflow.com/questions/459643/using-a-long-as-arraylist-index-in-java http://bbs.csdn.ne ...

  3. 在使用add()方法添加组件到容器时,必须指定将其放置在哪个区域中

    BorderLayout是Window.Frame和Dialog的默认布局管理器,其将容器分成North.South.East.West和Center 5个区域,每个区域只能放置一个组件. 在使用ad ...

  4. c#上传大文件方法

    客户端代码: /// <summary> /// 将本地文件上传到指定的服务器(HttpWebRequest方法) /// </summary> /// <param n ...

  5. win7 激活相关

    命令 slui 1 slui 2 slui 3 slui 4 slmgr.vbs 需打开的服务 需要开启software protection和 SPP Notification service这两个 ...

  6. RequireJS禁止缓存

    通过配置文件可以禁止加载缓存的JS文件, 这个在开发过程中非常有用具体做法如下 require.config({ paths: { "E":"/Scripts/MyMod ...

  7. Win10下Hyper-V设置网络连接

    具体方法如下. 1.点击虚拟交换机管理 2.创建虚拟交换机 选择内部 3.选择链接类型

  8. 关于vs2013中包含目录,以及库目录配置相对路径的问题

    记住一句话即可! 相对路径: 是相对于你的工程的*.vcxproj的路径!!!

  9. JAVA环境变量配置备忘

    jdk1.6以上就不需要配置classpath了:系统会自动帮你配置好 选择“高级”选项卡,点击“环境变量”:在“系统变量”中,设置3项属性,JAVA_HOME,PATH,CLASSPATH(大小写无 ...

  10. 【BZOJ1570】[JSOI2008]Blue Mary的旅行 动态加边网络流

    [BZOJ1570][JSOI2008]Blue Mary的旅行 Description 在一段时间之后,网络公司终于有了一定的知名度,也开始收到一些订单,其中最大的一宗来自B市.Blue Mary决 ...