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

父节点

----叶子节点

----子节点一

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

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

----子节点二

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

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

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

如何得到父节点

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

/**
	 * 向旗添加
	 * 实现将得到的目录的所有子目录和目录本身变成ID的LIST
	 * @param 目录ID
	 * @return  目录与其所有子目录组成的ID的LIST
	 * @throws ServiceException
	 */
	public List<Long> getAllPisTCategoryStr(PisTCategory obj)throws ServiceException{
		List<PisTCategory> listAll = new ArrayList<PisTCategory>();
		List<Long> list = new ArrayList<Long>();
		listAll = getAllPisTCategoryByParentId(obj);
	    if(listAll.size()!=0){
	         for(int i =0;i<listAll.size();i++){
	        	 list.add(listAll.get(i).getId());
	         }
	    }
	    return list;
	}
	
	/**
	 * 向旗添加
	 * 实现根据目录ID得到其所有子目录list
	 * @param 目录ID
	 * @return 所有子目录list
	 * @throws ServiceException
	 */
	public List<PisTCategory> getAllPisTCategoryByParentId(PisTCategory obj)throws ServiceException{
		List<PisTCategory> listAll = new ArrayList<PisTCategory>();
		List<PisTCategory> list = new ArrayList<PisTCategory>();
		List<PisTCategory> listtest = new ArrayList<PisTCategory>();
		try{
			if(obj.getLeaf() != null){
			if(obj.getLeaf()==1){
				listAll.add(obj);
				return listAll;
			}}
			obj.setLeaf(null);
			list =  getPisTCategoryByParentId_Leaf(obj);
			if(list.size()!=0){
			for(int i=0;i<list.size();i++){
					PisTCategory instance = new PisTCategory();
					instance = list.get(i);
					instance.setParentId(instance.getId());
					listtest = getAllPisTCategoryByParentId(instance);
					listAll.addAll(listtest);
			}
			
			}
		}catch(Exception e){
			e.printStackTrace();
			throw new ServiceException(this.getClass().getName()+e.getMessage());
		}
		return listAll;
	}

 

/**
	 * 向旗添加
	 * 实现根据父目录查询其子目录及其的所有商品信息
	 * @param categoryId
	 * @return
	 * @throws ServiceException
	 */
	public List<PisTProduct> getListPisTProductByCategoryId(Long categoryId)throws ServiceException{
		PisTCategory obj = new PisTCategory();
		obj = pisTCategoryService.getPisTCategoryById(categoryId);
		obj.setParentId(obj.getId());
		List<Long> list = pisTCategoryService.getAllPisTCategoryStr(obj);
		System.out.println(list.size());
		GetProductBySiteCategoryQueryObject qo = new GetProductBySiteCategoryQueryObject();
		qo.setSiteId(null);
		qo.setCategorys(list);
		try {
			return pisTProductDao.getListPisTProductBySiteId_CategoryIDs(qo);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}
	
 
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值