dfs
This commit is contained in:
parent
bc966cf137
commit
b60463cc0b
@ -19,8 +19,8 @@ public class Main {
|
||||
Deque<Long> stack = new ArrayDeque<>();
|
||||
AtomicBoolean flag = new AtomicBoolean(false);
|
||||
// contractTypeDFS(841339223809196032L, typeTree, stack, flag);
|
||||
Deque<Long> longs = treeFindPath(typeTree, 841338235396292608L, stack);
|
||||
System.out.println(stack);
|
||||
Deque<Long> longs = treeFindPath(typeTree, "LX844150457751306240", stack);
|
||||
System.out.println(longs);
|
||||
}
|
||||
|
||||
public static Deque<Long> treeFindPath(List<Type> tree, Long id, Deque<Long> stack){
|
||||
@ -44,6 +44,27 @@ public class Main {
|
||||
}
|
||||
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){
|
||||
ArrayList<Type> tree = Lists.newArrayList();
|
||||
if(CollectionUtils.isNotEmpty(list)){
|
||||
|
||||
Loading…
Reference in New Issue
Block a user