zoukankan      html  css  js  c++  java
  • 求树中两个节点的最低公共祖先

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

    必须下面的图,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
    ", "I","G",p1.val);
            }
            else{
                System.out.format("%s and %s's neariest common father null
    ", "I","G");
            }
            
            TreeNode p2=getNeariestCommonFather(N1,N9,N10);
            if(p1!=null){
                System.out.format("The neariest common father of %s and %s is %s
    ", "I","J",p2.val);
            }
            else{
                System.out.format("%s and %s's neariest common father null
    ", "I","J");
            }
            
            TreeNode p3=getNeariestCommonFather(N1,N9,N9);
            if(p1!=null){
                System.out.format("The neariest common father of %s and %s is %s
    ", "I","I",p3.val);
            }
            else{
                System.out.format("%s and %s's neariest common father null
    ", "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>();
        }
    }
  • 相关阅读:
    第三部分 学习shell与shell scipt---第九章 vim程序编辑器
    端口映射与容器互联
    在web工程中如何查看编译后class路径
    Spring配置JDBC连接Orcale、MySql、sqlserver
    org.springframework.beans.factory.BeanNotOfRequiredTypeException错误
    windows系统中如何启动两个tomcat
    eclipse+maven搭建SSM框架
    windows下安装和配置多个版本的JDK
    java环境变量为1.7jdk为何在dos窗口中查看版本为1.8
    查看jdk、eclipse、tomcat的是32位还是64
  • 原文地址:https://www.cnblogs.com/orchid/p/3355784.html
Copyright © 2011-2022 走看看