用递归算法查找父节点下的所有叶子节点

父节点

----叶子节点

----子节点一

---------叶子节点

---------叶子节点

----子节点二

---------子节点三

----------------叶子节点

----------------叶子节点

如何得到父节点

采用递归算法,碰到叶子节点就加到列表里,不是叶子节点就对其进行循环再递归遍历

Java代码   收藏代码
  1. /** 
  2.      * 向旗添加 
  3.      * 实现将得到的目录的所有子目录和目录本身变成ID的LIST 
  4.      * @param 目录ID 
  5.      * @return  目录与其所有子目录组成的ID的LIST 
  6.      * @throws ServiceException 
  7.      */  
  8.     public List<Long> getAllPisTCategoryStr(PisTCategory obj)throws ServiceException{  
  9.         List<PisTCategory> listAll = new ArrayList<PisTCategory>();  
  10.         List<Long> list = new ArrayList<Long>();  
  11.         listAll = getAllPisTCategoryByParentId(obj);  
  12.         if(listAll.size()!=0){  
  13.              for(int i =0;i<listAll.size();i++){  
  14.                  list.add(listAll.get(i).getId());  
  15.              }  
  16.         }  
  17.         return list;  
  18.     }  
  19.       
  20.     /** 
  21.      * 向旗添加 
  22.      * 实现根据目录ID得到其所有子目录list 
  23.      * @param 目录ID 
  24.      * @return 所有子目录list 
  25.      * @throws ServiceException 
  26.      */  
  27.     public List<PisTCategory> getAllPisTCategoryByParentId(PisTCategory obj)throws ServiceException{  
  28.         List<PisTCategory> listAll = new ArrayList<PisTCategory>();  
  29.         List<PisTCategory> list = new ArrayList<PisTCategory>();  
  30.         List<PisTCategory> listtest = new ArrayList<PisTCategory>();  
  31.         try{  
  32.             if(obj.getLeaf() != null){  
  33.             if(obj.getLeaf()==1){  
  34.                 listAll.add(obj);  
  35.                 return listAll;  
  36.             }}  
  37.             obj.setLeaf(null);  
  38.             list =  getPisTCategoryByParentId_Leaf(obj);  
  39.             if(list.size()!=0){  
  40.             for(int i=0;i<list.size();i++){  
  41.                     PisTCategory instance = new PisTCategory();  
  42.                     instance = list.get(i);  
  43.                     instance.setParentId(instance.getId());  
  44.                     listtest = getAllPisTCategoryByParentId(instance);  
  45.                     listAll.addAll(listtest);  
  46.             }  
  47.               
  48.             }  
  49.         }catch(Exception e){  
  50.             e.printStackTrace();  
  51.             throw new ServiceException(this.getClass().getName()+e.getMessage());  
  52.         }  
  53.         return listAll;  
  54.     }  

 

Java代码   收藏代码
  1. /** 
  2.      * 向旗添加 
  3.      * 实现根据父目录查询其子目录及其的所有商品信息 
  4.      * @param categoryId 
  5.      * @return 
  6.      * @throws ServiceException 
  7.      */  
  8.     public List<PisTProduct> getListPisTProductByCategoryId(Long categoryId)throws ServiceException{  
  9.         PisTCategory obj = new PisTCategory();  
  10.         obj = pisTCategoryService.getPisTCategoryById(categoryId);  
  11.         obj.setParentId(obj.getId());  
  12.         List<Long> list = pisTCategoryService.getAllPisTCategoryStr(obj);  
  13.         System.out.println(list.size());  
  14.         GetProductBySiteCategoryQueryObject qo = new GetProductBySiteCategoryQueryObject();  
  15.         qo.setSiteId(null);  
  16.         qo.setCategorys(list);  
  17.         try {  
  18.             return pisTProductDao.getListPisTProductBySiteId_CategoryIDs(qo);  
  19.         } catch (Exception e) {  
  20.             // TODO Auto-generated catch block  
  21.             e.printStackTrace();  
  22.         }  
  23.         return null;  
  24.     }  
  25.       

转载于:https://www.cnblogs.com/chuanqimessi/p/5278698.html

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值