@Test public void shouldReturnEmptyListIfTreeIsNull() { List<BinaryNode> result = toList.transform(null); assertTrue(result.isEmpty()); }
public List<BinaryNode> transform(BinaryNode tree) { if (tree == null) { return Collections.EMPTY_LIST; } List<BinaryNode> nodesByLevel = new LinkedList<BinaryNode>(); Queue<BinaryNode> stack = new LinkedList<BinaryNode>(); stack.add(tree); while (!stack.isEmpty()) { BinaryNode node = stack.remove(); nodesByLevel.add(node); if (node.getLeft() != null) { stack.add(node.getLeft()); } if (node.getRight() != null) { stack.add(node.getRight()); } } return nodesByLevel; }
TreeToListByLevel { public List<BinaryNode> transform(BinaryNode tree) { if (tree == null) { return Collections.EMPTY_LIST; } List<BinaryNode> nodesByLevel = new LinkedList<BinaryNode>(); Queue<BinaryNode> stack = new LinkedList<BinaryNode>(); stack.add(tree); while (!stack.isEmpty()) { BinaryNode node = stack.remove(); nodesByLevel.add(node); if (node.getLeft() != null) { stack.add(node.getLeft()); } if (node.getRight() != null) { stack.add(node.getRight()); } } return nodesByLevel; } }
TreeToListByLevel { public List<BinaryNode> transform(BinaryNode tree) { if (tree == null) { return Collections.EMPTY_LIST; } List<BinaryNode> nodesByLevel = new LinkedList<BinaryNode>(); Queue<BinaryNode> stack = new LinkedList<BinaryNode>(); stack.add(tree); while (!stack.isEmpty()) { BinaryNode node = stack.remove(); nodesByLevel.add(node); if (node.getLeft() != null) { stack.add(node.getLeft()); } if (node.getRight() != null) { stack.add(node.getRight()); } } return nodesByLevel; }  }
TreeToListByLevel { public List<BinaryNode> transform(BinaryNode tree) { if (tree == null) { return Collections.EMPTY_LIST; } List<BinaryNode> nodesByLevel = new LinkedList<BinaryNode>(); Queue<BinaryNode> stack = new LinkedList<BinaryNode>(); stack.add(tree); while (!stack.isEmpty()) { BinaryNode node = stack.remove(); nodesByLevel.add(node); if (node.getLeft() != null) { stack.add(node.getLeft()); } if (node.getRight() != null) { stack.add(node.getRight()); } } return nodesByLevel; }  List<BinaryNode> transform(BinaryNode tree); }
TreeToListByLevel { public List<BinaryNode> transform(BinaryNode tree) { if (tree == null) { return Collections.EMPTY_LIST; } List<BinaryNode> nodesByLevel = new LinkedList<BinaryNode>(); Queue<BinaryNode> stack = new LinkedList<BinaryNode>(); stack.add(tree); while (!stack.isEmpty()) { BinaryNode node = stack.remove(); nodesByLevel.add(node); if (node.getLeft() != null) { stack.add(node.getLeft()); } if (node.getRight() != null) { stack.add(node.getRight()); } } return nodesByLevel; }  List<BinaryNode> transform(BinaryNode tree);  }