码迷,mamicode.com
首页 > 其他好文 > 详细

lintcode474- Lowest Common Ancestor II- easy

时间:2017-10-12 10:13:51      阅读:154      评论:0      收藏:0      [点我收藏+]

标签:用两个   中国人   tco   ini   array   parent   lca   int   att   

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;
    }
}

 

lintcode474- Lowest Common Ancestor II- easy

标签:用两个   中国人   tco   ini   array   parent   lca   int   att   

原文地址:http://www.cnblogs.com/jasminemzy/p/7654348.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!