236. Lowest Common Ancestor of a Binary Tree

package com.cj.algorithm;


import java.util.ArrayList;
import java.util.List;


public class LowestCommonAncestorofaBinaryTree {


/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub


}


/*
* Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree.


According to the definition of LCA on Wikipedia: “The lowest common ancestor is defined 
between two nodes v and w as the lowest node in T that has both v and w as descendants 
(where we allow a node to be a descendant of itself).”


        _______3______
       /              \
    ___5__          ___1__
   /      \        /      \
   6      _2       0       8
         /  \
         7   4
For example, the lowest common ancestor (LCA) of nodes 5 and 1 is 3. Another example is LCA of nodes 5 and 4 is 5, 
since a node can be a descendant of itself according to the LCA definition.
*/
/*
*  如果是普通二叉树, 而不是BST.  则应该遍历节点, 先找到p,q. 同时记录下从root到该几点的路径. 
*    之后比较路径,最后一个相同的节点便是LCA.
*/
List<List<TreeNode>> result=new ArrayList<List<TreeNode>>();//存储路径
List<TreeNode> path=new ArrayList<TreeNode>();//存储路径
public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
TreeNode node=null;
if (root == null|| p==null||q==null) return node;
List<TreeNode> pathp=new ArrayList<TreeNode>();//存储路径
List<TreeNode> pathq=new ArrayList<TreeNode>();//存储路径

pathp.add(root);  
pathq.add(root);  

getPath(root,p,pathp);
getPath(root,p,pathq);

int pn,qn;
pn=result.get(0).size();
qn=result.get(1).size();
int i=0;
int j=0;
while(i<pn&&j<qn){
if(result.get(0).get(i)==result.get(1).get(j)){
node = result.get(0).get(i);
i++;
j++;
}
else{
break;
}
}
return node;
        
    }


private void getPath1(TreeNode root,TreeNode p,List<TreeNode> path) {
// TODO Auto-generated method stub
if(root == p){
return;
}
else 
{
if(root.left!=null){
path.add(root.left);
getPath1(root.left,p,path);
path.remove(path.size()-1);//递归
}
if(root.right!=null){
path.add(root.right);
getPath1(root.right,p,path);
path.remove(path.size()-1);//递归
}
}

}
public TreeNode lowestCommonAncestor1(TreeNode root, TreeNode p, TreeNode q) {  
    if(root==null || p==null || q==null) return null;  
      
    List<TreeNode> pathp = new ArrayList<TreeNode>();  
    List<TreeNode> pathq = new ArrayList<TreeNode>();  
    pathp.add(root);  
    pathq.add(root);  
      
    getPath(root, p, pathp);  
    getPath(root, q, pathq);  
      
    TreeNode lca = null;  
    for(int i=0; i<pathp.size() && i<pathq.size(); i++) {  
        if(pathp.get(i) == pathq.get(i)) lca = pathp.get(i);  
        else break;  
    }  
    return lca;  
}  
  
private boolean getPath(TreeNode root, TreeNode n, List<TreeNode> path) {  
    if(root==n) {  
        return true;  
    }  
      
    if(root.left!=null) {  
        path.add(root.left);  
        if(getPath(root.left, n, path)) return true;  
        path.remove(path.size()-1);  
    }  
      
    if(root.right!=null) {  
        path.add(root.right);  
        if(getPath(root.right, n, path)) return true;  
        path.remove(path.size()-1);  
    }  
      
    return false;  
}  
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值