Stack<TreeNode<T>> stack = new Stack<>(); //压栈 stack.push(node); //循环出栈() while (!stack.isEmpty()){ TreeNode current = stack.pop(); result.add(current); List<TreeNode> childrens = current.getChildren(); if (CollectionUtils.isEmpty(childrens)){ continue; } for (TreeNode child:childrens){ if (!ObjectUtils.isEmpty(child)){ stack.push(child); } } } return result; }
发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/232673.html原文链接:https://javaforall.net
