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>();
        }
    }
  • 相关阅读:
    php面试题目
    JavaScript表单处理的返回值问题
    超链接在javascript:void(0)时没有事件响应
    php 两个美元符号:可变变量
    [Ubuntu] lampp安装Zend Framework
    [Ubuntu] 安装字体
    php中bindValue 和 bindParam 的区别
    php遍历文件夹(获得文件名)
    php输出一段字符块
    PHP 全角和半角转换函数
  • 原文地址:https://www.cnblogs.com/orchid/p/3355784.html
Copyright © 2011-2022 走看看