zoukankan      html  css  js  c++  java
  • **Lowest Common Ancestor of Two Nodes in a Binary Tree

    Given the root and two nodes in a Binary Tree. Find the lowest common ancestor(LCA) of the two nodes.

    The lowest common ancestor is the node with largest depth which is the ancestor of both nodes.

    Example

    For the following binary tree:

      4
     / 
    3   7
       / 
      5   6
    

    LCA(3, 5) = 4

    LCA(5, 6) = 7

    LCA(6, 7) = 7

    解法一: 

     1 /**
     2  * 本代码由九章算法编辑提供。没有版权欢迎转发。
     3  * - 九章算法致力于帮助更多中国人找到好的工作,教师团队均来自硅谷和国内的一线大公司在职工程师。
     4  * - 现有的面试培训课程包括:九章算法班,系统设计班,BAT国内班
     5  * - 更多详情请见官方网站:http://www.jiuzhang.com/
     6  */
     7 
     8 Version 1: Traditional Method
     9 
    10 public class Solution {
    11     private ArrayList<TreeNode> getPath2Root(TreeNode node) {
    12         ArrayList<TreeNode> list = new ArrayList<TreeNode>();
    13         while (node != null) {
    14             list.add(node);
    15             node = node.parent;
    16         }
    17         return list;
    18     }
    19     public TreeNode lowestCommonAncestor(TreeNode node1, TreeNode node2) {
    20         ArrayList<TreeNode> list1 = getPath2Root(node1);
    21         ArrayList<TreeNode> list2 = getPath2Root(node2);
    22         
    23         int i, j;
    24         for (i = list1.size() - 1, j = list2.size() - 1; i >= 0 && j >= 0; i--, j--) {
    25             if (list1.get(i) != list2.get(j)) {
    26                 return list1.get(i).parent;
    27             }
    28         }
    29         return list1.get(i+1);
    30     }
    31 }
    32 

     解法二:分治法

    public class Solution {
        /**
         * @param root: The root of the binary search tree.
         * @param A and B: two nodes in a Binary.
         * @return: Return the least common ancestor(LCA) of the two nodes.
         */
        public TreeNode lowestCommonAncestor(TreeNode root, TreeNode A, TreeNode B) {
            if (root == null){
                return null;
            }
            if (root == A || root == B){
                return root;//当找到一中一个node, 向上传递这个node
            }
            TreeNode left = lowestCommonAncestor(root.left, A, B);
            TreeNode right = lowestCommonAncestor(root.right, A, B);
            if (left != null && right != null){//check左右,如果左右分别包含两个node则这个root就是LCA, 向上传递这个node
                return root;
            } else if (left != null){//只有左边有node,向上传递此node
                return left;
            } else if (right != null){
                return right;
            } else{//左右边都没有node 传递null
                return null;
            }
        }
    }

     video: http://www.bittiger.io/videos/TLfAyc3Hxfaxp2Fs9/Jj33gh5tbGcFyKCWa

    reference:http://ryanleetcode.blogspot.com/2015/04/lowest-common-ancestor.html

  • 相关阅读:
    yum下载安装mysql服务
    windows编写sh脚本在linux上不能执行
    ectouch第二讲之 文件结构
    ecshop第一讲之安装
    class id 区别
    thinkphp模板中截取中文字符串的方法分享
    CI 配置验证规则
    CodeIgniter配置之config
    codeigniter中base_url和site_url
    使用phpmyadmin导入SQL数据报错:#1062
  • 原文地址:https://www.cnblogs.com/hygeia/p/4772080.html
Copyright © 2011-2022 走看看