首页 > 其他分享 >逆向递归,给一个完整的树,找对应的部门树

逆向递归,给一个完整的树,找对应的部门树

时间:2022-11-08 16:00:27浏览次数:31  
标签:deptId 逆向 return 递归 Tree tree children 对应

递归找到树 找到部门 树

构建一棵树 很简单,只要有parentId,很简单递归 就能构建好这棵树,今天来讲 怎么样 递归从树中 找到这个 对应 id 的树,比如传入 部门id 找到 这个部门树,逆向递归。

/**
	 * 获取用户部门树
	 * @param treeList
	 * @return
	 */
	private Tree<Long> getUserDeptTree(List<Tree<Long>> treeList,Long deptId){
		if(!CollectionUtils.isEmpty(treeList)) {
			for (int i = 0; i < treeList.size(); i++) {
				Tree<Long> tree = treeList.get(i);
				List<Tree<Long>> children = tree.getChildren();
				if (Objects.equals(tree.getId(), deptId) && !CollectionUtils.isEmpty(children)) {
					return tree;
				}
				return getBuildUserDeptTree(children,deptId);
			}

		}
		return new Tree<>();
	}
	
	/**
	 * 递归找到部门树
	 * @param children
	 * @param deptId
	 * @return
	 */
	private Tree<Long> getBuildUserDeptTree(List<Tree<Long>> children,Long deptId){
		Tree<Long> result =  new Tree<>();
		if(!CollectionUtils.isEmpty(children)){
			for (int i = 0; i < children.size(); i++) {
				Tree<Long> tree = children.get(i);
				List<Tree<Long>> childrenTree = tree.getChildren();
				if (Objects.equals(tree.getId(), deptId) && !CollectionUtils.isEmpty(children)) {
					return tree;
				}
				Tree<Long> userDeptTree = getBuildUserDeptTree(childrenTree, deptId);
				if(!CollectionUtils.isEmpty(userDeptTree)){
					return userDeptTree;
				}
			}
		}

		return result;
	}

标签:deptId,逆向,return,递归,Tree,tree,children,对应
From: https://www.cnblogs.com/lyc88/p/16869994.html

相关文章

  • 为什么你学不会递归?谈谈我的经验
    本文已收录到 GitHub·AndroidFamily,有Android进阶知识体系,欢迎Star。技术和职场问题,请关注公众号[彭旭锐]进Android面试交流群。前言大家好,我是小彭。今......
  • Windows下将压缩包隐写到图片中以及相对应的提取
    Windows下将压缩包隐写到图片中以及相对应的提取其实原理非常的简单,就是进行一个压缩,只不过让它看起来像图片一样;下面是效果:实现的原理是非常简单的,使用了Windows下的命......
  • 『数据结构与算法』解读递归算法!
    文章目录​​一.什么是递归​​​​1.1.递归的定义​​​​1.2.何时使用递归​​​​1.3.递归模型​​​​二.递归算法的设计​​​​2.1.递归算法设计的步骤​​​​......
  • 逆向工程-代码生成器
    packagecom.atguigu.demo;importcom.baomidou.mybatisplus.annotation.DbType;importcom.baomidou.mybatisplus.annotation.IdType;importcom.baomidou.mybatisplus.ge......
  • 不知道为什么递归失败 二叉树 演我?
    不知道为什么不能够递归搞明白了再更啊哈哈哈怎么突然就行了准备截一张失败的图来着然后突然就出来了也不知道之前为什么失败  非递归的今天晚上应该写不出来了......
  • 逆向简单题记录
    关于最后的v7处在esp+40h的位置esp+40h-41=esp+17h此时再加5*v3[25]+v4可得到为v3数组中某值v3为行,v4为列......
  • 001.项目初始化,生成逆向文件
    1.整合Mybatis1.1在pom.xml中添加文件<dependency><groupId>org.mybatis.spring.boot</groupId><artifactId>mybatis-spring-boot-......
  • 递归转迭代
    参考参考文章例题对称二叉树反转链表翻转二叉树验证二叉搜索树......
  • 递归
    递归的概念简单的说:递归就是方法自己调用自己,每次调用时传入不同的变量.递归有助于编程者解决复杂的问题,同时可以让代码变得简洁。递归调用机制以打印问题和阶乘问......
  • 使用递归获取数组最大值。(有图)
    packageclass03;importjava.util.Arrays;/***使用递归获取数组最大值*只是用这个获取数组最大值的例子,来理解递归。*/publicclassCode08_GetMax{p......