result = new ArrayList<>();
Map<Long, DsrBaseCommentDTO> map = new HashMap<>();
//最好时间复杂度算法
for (DsrBaseCommentDTO c : all) {
if (c.getParentId() == null) {
result.add(c);
}
map.put(c.getId(), c);
}
for (DsrBaseCommentDTO c : all) {
if (c.getParentId() != null) {
DsrBaseCommentDTO parent = map.get(c.getParentId());
if (parent != null) {
if (parent.getChild() == null) {
parent.setChild(new ArrayList<>());
}
parent.getChild().add(c);
}
}
}
【树级结构通用--最好时间复杂度算法】
于 2023-11-15 15:12:40 首次发布