1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
| public static List<List<String>> queueFloorAndPrint(Node head){ if (head==null) { return null; } List<List<String>> list = new ArrayList<>(); Queue<Node> q = new LinkedList<>(); q.add(head); List<String> tmp; while (!q.isEmpty()){ int size = q.size(); tmp=new ArrayList<>(); while (size-->0){ Node node = q.remove(); System.out.print(node.value); tmp.add(node.value); if (node.left!=null) { q.add(node.left); } if (node.right!=null) { q.add(node.right); } } list.add(tmp); } return list; }
|