@Test public void shouldReturnTheSameNodeInTheListIfTheTreeJustContainsOneElementUsingQueue() { BinaryNode<Integer> root = new BinaryNode<Integer>(0); List<BinaryNode> result = binaryTreeByLevel.getUsingQueue(root); List<BinaryNode<Integer>> expectedList = Arrays.asList(root); assertEquals(expectedList, result); }
public List<BinaryNode> getUsingQueue(BinaryNode root) { validateBinaryNode(root); List<BinaryNode> result = new LinkedList<BinaryNode>(); Queue<BinaryNode> queue = new LinkedList<BinaryNode>(); queue.add(root); while (!queue.isEmpty()) { BinaryNode binaryNode = queue.remove(); result.add(binaryNode); if (binaryNode.getLeft() != null) queue.add(binaryNode.getLeft()); if (binaryNode.getRight() != null) queue.add(binaryNode.getRight()); } return result; }
BinaryTreeByLevel { public List<BinaryNode> getUsingQueue(BinaryNode root) { validateBinaryNode(root); List<BinaryNode> result = new LinkedList<BinaryNode>(); Queue<BinaryNode> queue = new LinkedList<BinaryNode>(); queue.add(root); while (!queue.isEmpty()) { BinaryNode binaryNode = queue.remove(); result.add(binaryNode); if (binaryNode.getLeft() != null) queue.add(binaryNode.getLeft()); if (binaryNode.getRight() != null) queue.add(binaryNode.getRight()); } return result; } }
BinaryTreeByLevel { public List<BinaryNode> getUsingQueue(BinaryNode root) { validateBinaryNode(root); List<BinaryNode> result = new LinkedList<BinaryNode>(); Queue<BinaryNode> queue = new LinkedList<BinaryNode>(); queue.add(root); while (!queue.isEmpty()) { BinaryNode binaryNode = queue.remove(); result.add(binaryNode); if (binaryNode.getLeft() != null) queue.add(binaryNode.getLeft()); if (binaryNode.getRight() != null) queue.add(binaryNode.getRight()); } return result; }  }
BinaryTreeByLevel { public List<BinaryNode> getUsingQueue(BinaryNode root) { validateBinaryNode(root); List<BinaryNode> result = new LinkedList<BinaryNode>(); Queue<BinaryNode> queue = new LinkedList<BinaryNode>(); queue.add(root); while (!queue.isEmpty()) { BinaryNode binaryNode = queue.remove(); result.add(binaryNode); if (binaryNode.getLeft() != null) queue.add(binaryNode.getLeft()); if (binaryNode.getRight() != null) queue.add(binaryNode.getRight()); } return result; }  List<BinaryNode> getUsingQueue(BinaryNode root); List<BinaryNode> getWithoutAdditionalDataStructures(BinaryNode root); }
BinaryTreeByLevel { public List<BinaryNode> getUsingQueue(BinaryNode root) { validateBinaryNode(root); List<BinaryNode> result = new LinkedList<BinaryNode>(); Queue<BinaryNode> queue = new LinkedList<BinaryNode>(); queue.add(root); while (!queue.isEmpty()) { BinaryNode binaryNode = queue.remove(); result.add(binaryNode); if (binaryNode.getLeft() != null) queue.add(binaryNode.getLeft()); if (binaryNode.getRight() != null) queue.add(binaryNode.getRight()); } return result; }  List<BinaryNode> getUsingQueue(BinaryNode root); List<BinaryNode> getWithoutAdditionalDataStructures(BinaryNode root);  }