zoukankan      html  css  js  c++  java
  • 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 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不在同一边,那就表示已经找到第一个公共祖先。

     1 class Solution {
     2     public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
     3         if(!cover(root,p)||!cover(root,q)) return null;
     4         return LCAhelp(root,p,q);
     5     }
     6     private  TreeNode LCAhelp(TreeNode root,TreeNode p,TreeNode q ){
     7         if(root==null) return null;
     8         if(root==p||root==q) return root;
     9         boolean q_is_on_left = cover(root.left,q);
    10         boolean p_is_on_left = cover(root.left,p);
    11         
    12         //分立两边 
    13         if(q_is_on_left!=p_is_on_left) return root;
    14         
    15         //在一边
    16         else{
    17             if(q_is_on_left)
    18                 return LCAhelp(root.left,p,q);
    19             else
    20                 return LCAhelp(root.right,p,q);
    21         }
    22     }
    23     private boolean cover(TreeNode root,TreeNode p){
    24         //检查p是不是root的孙子
    25 
    26         if(root==null) return false;
    27         if(root==p) return true;
    28         return cover(root.left,p)||cover(root.right,p);
    29     }
    30 }

    解题思路

    • Divide & Conquer 的思路
    • 如果root为空,则返回空
    • 如果root等于其中某个node,则返回root
    • 如果上述两种情况都不满足,则divide,左右子树分别调用该方法
    • Divide & Conquer中这一步要考虑清楚,本题三种情况
    • 如果leftright都有结果返回,说明root是最小公共祖先
    • 如果只有left有返回值,说明left的返回值是最小公共祖先
    • 如果只有�right有返回值,说明�right的返回值是最小公共祖先


     1 class Solution {
     2     public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
     3         if(root ==null || p==root||q==root) return root;
     4         TreeNode lp = lowestCommonAncestor(root.left,p,q);
     5         TreeNode rp = lowestCommonAncestor(root.right,p,q);
     6         if(lp!=null&&rp!=null) return root;
     7         if(lp==null&&rp!=null) return rp;
     8         if(lp!=null&&rp==null) return lp;
     9         return null;
    10     }
    11 }
  • 相关阅读:
    【转载】中文分词整理
    【转载】浅谈事件冒泡与事件捕获
    【转载】SpringCloud-Eurek 心跳阈值说明
    【转载】Linux下查看CPU、内存占用率
    Linux内存、性能诊断中vmstat命令的详解
    【转载】springboot四 全局异常处理
    【转载】linux系统时间自动同步:ntp
    springboot整合三 共享session,集成springsession
    git把一个分支上的某个提交合并到另一个分支
    VS Code打开新的文件会覆盖窗口中的,怎么改
  • 原文地址:https://www.cnblogs.com/zle1992/p/8419511.html
Copyright © 2011-2022 走看看