递归找到树 找到部门 树
构建一棵树 很简单,只要有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