求树中两个节点的最低公共祖先

这个树没有规定是二叉树。思路就是先求根到两个节点的路径,然后根据两条路径求的最低公共祖先。

必须下面的图,I和G的最低祖先是A,I和J的最低公共祖先是D。

完成的java代码如下:

import java.util.*;
public class NearistCommonFather {

    public TreeNode getNeariestCommonFather(TreeNode h,TreeNode n1,TreeNode n2){
        if(h==null||n1==null||n2==null){
            return null;
        }
        LinkedList<TreeNode> path1=new LinkedList<TreeNode>();
        LinkedList<TreeNode> path2=new LinkedList<TreeNode>();
        
        boolean f1=getPath(h,n1,path1);
        boolean f2=getPath(h,n2,path2);
        
        if(!f1||!f2){
            return null;
        }
        
        //get common father in the path
        TreeNode commonP=null;
        while(!path1.isEmpty()&&!path2.isEmpty()){
            TreeNode p1=path1.remove();
            TreeNode p2=path2.remove();
            if(p1==p2){
                commonP=p1;
            }
            else{
                break;
            }
        }
        return commonP;
    }
    //得到从根节点到指定节点的路径,存储在list中
    public boolean getPath(TreeNode h,TreeNode n,LinkedList<TreeNode> list){
        if(h==null){
            return false;
        }
        list.add(h);
        if(h==n){
            return true;
        }
        boolean finded=false;
        for(TreeNode chd :h.childList){
            if(finded){
                break;
            }
            finded=getPath(chd,n,list);
        }
        if(!finded){
            list.removeLast();
        }
        return finded;
    }
    public void test(){
        TreeNode N1=new TreeNode("A");
        TreeNode N2=new TreeNode("B");
        TreeNode N3=new TreeNode("C");
        TreeNode N4=new TreeNode("D");
        TreeNode N5=new TreeNode("E");
        TreeNode N6=new TreeNode("F");
        TreeNode N7=new TreeNode("G");
        TreeNode N8=new TreeNode("H");
        TreeNode N9=new TreeNode("I");
        TreeNode N10=new TreeNode("J");    
        
        N1.childList.add(N2);
        N1.childList.add(N3);
        
        N2.childList.add(N4);
        N2.childList.add(N5);
        
        N3.childList.add(N6);
        N3.childList.add(N7);
        N3.childList.add(N8);
        
        N4.childList.add(N9);
        N4.childList.add(N10);
        
        TreeNode p1=getNeariestCommonFather(N1,N7,N9);
        if(p1!=null){
            System.out.format("The neariest common father of %s and %s is %s\n", "I","G",p1.val);
        }
        else{
            System.out.format("%s and %s's neariest common father null\n", "I","G");
        }
        
        TreeNode p2=getNeariestCommonFather(N1,N9,N10);
        if(p1!=null){
            System.out.format("The neariest common father of %s and %s is %s\n", "I","J",p2.val);
        }
        else{
            System.out.format("%s and %s's neariest common father null\n", "I","J");
        }
        
        TreeNode p3=getNeariestCommonFather(N1,N9,N9);
        if(p1!=null){
            System.out.format("The neariest common father of %s and %s is %s\n", "I","I",p3.val);
        }
        else{
            System.out.format("%s and %s's neariest common father null\n", "I","I");
        }
        
    }
    
    public static void main(String[] args){
        NearistCommonFather ncf=new NearistCommonFather();
        ncf.test();
    }
}


class TreeNode{
    String val;
    ArrayList<TreeNode> childList;
    TreeNode(String v){
        val=v;
        childList=new ArrayList<TreeNode>();
    }
}

 

posted @ 2013-10-07 17:59  orchid  阅读(451)  评论(0编辑  收藏  举报