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;
        }
    }
  • 相关阅读:
    Unity 3D第三人称视角、用途广泛限定角度(视角不能360度翻转)
    Android Studio安卓导出aar包与Unity 3D交互
    Unity 3D调用Windows打开、保存窗口、文件浏览器
    安卓与Unity交互之-Android Studio创建Module库模块教程
    Unity 3D与Android Studio安卓交互之-导出jar包
    C#字符串string以及相关内置函数
    Unity 3D委托entrust
    数据结构与算法学习一
    .NET Core学习一--Powered by .NET Core on Kubernetes
    easyui.form
  • 原文地址:https://www.cnblogs.com/jasminemzy/p/7654348.html
Copyright © 2011-2022 走看看