zoukankan      html  css  js  c++  java
  • lintcode474- Lowest Common Ancestor II- easy

    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.

    The node has an extra attribute parent which point to the father of itself. The root's parent is null.

    Example

    For the following binary tree:

      4
     / 
    3   7
       / 
      5   6
    

    LCA(3, 5) = 4

    LCA(5, 6) = 7

    LCA(6, 7) = 7

    1.用hashset辅助做。思想是AB各自向上回溯,如果第一次碰到了一样的点那就是lca.所以可以先回溯a,都存到set里,再在回溯b的时候每次看访问的点在不在a的父亲们里面了。

    2.用两个arraylist做。思想是把ab各自到root的路径全都打印出来。那么这个root如果倒过来看的话,前面一部分都会是一样的,第一个不一样的点的上面那个分岔点就是lca了。

    1.我的代码

    /**
     * Definition of ParentTreeNode:
     * 
     * class ParentTreeNode {
     *     public ParentTreeNode parent, left, right;
     * }
     */
    
    
    public class Solution {
        /*
         * @param root: The root of the tree
         * @param A: node in the tree
         * @param B: node in the tree
         * @return: The lowest common ancestor of A and B
         */
        public ParentTreeNode lowestCommonAncestorII(ParentTreeNode root, ParentTreeNode A, ParentTreeNode B) {
            // write your code here
            
            ParentTreeNode result = null;
            Set<ParentTreeNode> set = new HashSet<ParentTreeNode>();
            
            ParentTreeNode aParent = A;
            ParentTreeNode bParent = B;
            
            while (aParent != null) {
                set.add(aParent);
                aParent = aParent.parent;
            }
            
            while (bParent != null) {
                if (set.contains(bParent)) {
                    result = bParent;
                    break;
                }
                bParent = bParent.parent;
            }
            
            return result;
            
        }
    }

    2.九章算法上的参考代码

    /**
    * 本参考程序来自九章算法,由 @九章算法 提供。版权所有,转发请注明出处。
    * - 九章算法致力于帮助更多中国人找到好的工作,教师团队均来自硅谷和国内的一线大公司在职工程师。
    * - 现有的面试培训课程包括:九章算法班,系统设计班,算法强化班,Java入门与基础算法班,Android 项目实战班,
    * - Big Data 项目实战班,算法面试高频题班, 动态规划专题班
    * - 更多详情请见官方网站:http://www.jiuzhang.com/?source=code
    */ 
    
    /**
     * Definition of ParentTreeNode:
     * 
     * class ParentTreeNode {
     *     public ParentTreeNode parent, left, right;
     * }
     */
    public class Solution {
        /**
         * @param root: The root of the tree
         * @param A, B: Two node in the tree
         * @return: The lowest common ancestor of A and B
         */
        public ParentTreeNode lowestCommonAncestorII(ParentTreeNode root,
                                                     ParentTreeNode A,
                                                     ParentTreeNode B) {
            ArrayList<ParentTreeNode> pathA = getPath2Root(A);
            ArrayList<ParentTreeNode> pathB = getPath2Root(B);
            
            int indexA = pathA.size() - 1;
            int indexB = pathB.size() - 1;
            
            ParentTreeNode lowestAncestor = null;
            while (indexA >= 0 && indexB >= 0) {
                if (pathA.get(indexA) != pathB.get(indexB)) {
                    break;
                }
                lowestAncestor = pathA.get(indexA);
                indexA--;
                indexB--;
            }
            
            return lowestAncestor;
        }
        
        private ArrayList<ParentTreeNode> getPath2Root(ParentTreeNode node) {
            ArrayList<ParentTreeNode> path = new ArrayList<>();
            while (node != null) {
                path.add(node);
                node = node.parent;
            }
            return path;
        }
    }
  • 相关阅读:
    C/C++编程可用的Linux自带工具
    安装gcc及其依赖
    Linux上编译hadoop-2.7.1的libhdfs.so和libhdfs.a
    gcc链接参数--whole-archive的作用
    jdb调试程序
    Exception in thread "main" java.lang.Error: Unresolved compilation problem
    动态规划与分治、备忘录的区别
    leetcode-unique paths
    LeetCode总结 -- 一维动态规划篇
    编程技巧
  • 原文地址:https://www.cnblogs.com/jasminemzy/p/7654348.html
Copyright © 2011-2022 走看看