@Test public void shouldReturnListWithTreeElementsByLevel() { BinaryNode<Integer> root = new BinaryNode<Integer>(0); BinaryNode<Integer> n1 = new BinaryNode<Integer>(1); BinaryNode<Integer> n2 = new BinaryNode<Integer>(2); BinaryNode<Integer> n3 = new BinaryNode<Integer>(3); root.setLeft(n1); root.setRight(n2); n1.setLeft(n3); List<BinaryNode> result = toList.transform(root); assertEquals(4, result.size()); assertEquals(root, result.get(0)); assertEquals(n1, result.get(1)); assertEquals(n2, result.get(2)); assertEquals(n3, result.get(3)); }
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);  }