This commit is contained in:
luming 2021-05-28 18:51:54 +08:00
parent bc966cf137
commit b60463cc0b

View File

@ -19,8 +19,8 @@ public class Main {
Deque<Long> stack = new ArrayDeque<>(); Deque<Long> stack = new ArrayDeque<>();
AtomicBoolean flag = new AtomicBoolean(false); AtomicBoolean flag = new AtomicBoolean(false);
// contractTypeDFS(841339223809196032L, typeTree, stack, flag); // contractTypeDFS(841339223809196032L, typeTree, stack, flag);
Deque<Long> longs = treeFindPath(typeTree, 841338235396292608L, stack); Deque<Long> longs = treeFindPath(typeTree, "LX844150457751306240", stack);
System.out.println(stack); System.out.println(longs);
} }
public static Deque<Long> treeFindPath(List<Type> tree, Long id, Deque<Long> stack){ public static Deque<Long> treeFindPath(List<Type> tree, Long id, Deque<Long> stack){
@ -44,6 +44,27 @@ public class Main {
} }
return emptyStack; return emptyStack;
} }
public static Deque<Long> treeFindPath(List<Type> tree, String code, Deque<Long> stack){
Deque<Long> emptyStack = new ArrayDeque<>();
if(CollectionUtils.isEmpty(tree)){
return emptyStack;
}
for (Type type : tree) {
System.out.println(type.getId());
stack.push(type.getId());
if(Objects.equals(type.getCode(),code)){
return stack;
}
if(CollectionUtils.isNotEmpty(type.getChildList())){
Deque<Long> longs = treeFindPath(type.getChildList(), code, stack);
if(!longs.isEmpty()){
return longs;
}
}
stack.pop();
}
return emptyStack;
}
public static List<Type> builldTree(List<Type> list){ public static List<Type> builldTree(List<Type> list){
ArrayList<Type> tree = Lists.newArrayList(); ArrayList<Type> tree = Lists.newArrayList();
if(CollectionUtils.isNotEmpty(list)){ if(CollectionUtils.isNotEmpty(list)){