Lowest Common Ancestor II

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

Example 1:

Input:{4,3,7,#,#,5,6},3,5
Output:4
Explanation:
     4
     / \
    3   7
       / \
      5   6
LCA(3, 5) = 4

Example 2:

Input:{4,3,7,#,#,5,6},5,6
Output:7
Explanation:
      4
     / \
    3   7
       / \
      5   6
LCA(5, 6) = 7

思路:有parent指针,就用hashset解决,从node A一路走上去,收集起来,然后从node B再往上走,如果遇见相同的则返回,否则最后返回root

/**
 * 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) {
        if(root == null) return null;
        HashSet<ParentTreeNode> hashset = new HashSet<ParentTreeNode>();
        ParentTreeNode node = A;
        while(node != root) {
            hashset.add(node);
            node = node.parent;
        }
        hashset.add(root);
        
        node = B;
        while(node != root) {
            if(hashset.contains(node)){
                return node;
            }
            node = node.parent;
        }
        return root;
    }
}

思路2:收集两条路线,然后从上往下走,遇见第一个不相等的index break,return break之前的node

/**
 * 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) {
        if(root == null) {
            return null;
        }
        
        List<ParentTreeNode> pathA = findPath(root, A);
        List<ParentTreeNode> pathB = findPath(root, B);
        
        return findLowestCommon(pathA, pathB);
    }
    
    private ParentTreeNode findLowestCommon(List<ParentTreeNode> pathA, List<ParentTreeNode> pathB) {
        int lenA = pathA.size();
        int lenB = pathB.size();
        int len = Math.min(lenA, lenB);
        ParentTreeNode res = pathA.get(0);
        for(int i = 0; i < len; i++) {
            if(pathA.get(i) == pathB.get(i)){
                res = pathA.get(i);
            }
        }
        return res;
    }
    
    private List<ParentTreeNode> findPath(ParentTreeNode root, ParentTreeNode A) {
        List<ParentTreeNode> list = new ArrayList<ParentTreeNode>();
        ParentTreeNode node = A;
        while(node != root) {
            list.add(0,node);
            node = node.parent;
        }
        list.add(0,root);
        return list;
    }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值