### Question:
LeftmostElementOfEachLevelInBinaryTree { public static List<Integer> findLeftmostElements(BinaryTree root) { if (root == null) { throw new IllegalArgumentException("Input tree is null"); } ArrayList<Integer> values = new ArrayList<>(); Queue<Object[]> queue = new LinkedList<>(); queue.add(new Object[]{root, 0}); int level = 0; while (!queue.isEmpty()) { Object[] obj = queue.poll(); BinaryTree currentTree = (BinaryTree) obj[0]; int currentLevel = (Integer) obj[1]; if (level == currentLevel) { values.add(currentTree.data); level++; } if (currentTree.left != null) { queue.add(new Object[]{currentTree.left, currentLevel + 1}); } if (currentTree.right != null) { queue.add(new Object[]{currentTree.right, currentLevel + 1}); } } return values; }  static List<Integer> findLeftmostElements(BinaryTree root);  }### Answer:
@Test(expected = IllegalArgumentException.class) public void testFindLeftmostElementsNullTree() { findLeftmostElements(null); }
@Test public void testFindLeftmostElements1() { List<Integer> actual = findLeftmostElements(new BinaryTree(null, null, 10)); List<Integer> expected = Collections.singletonList(10); assertThat(actual, is(expected)); }
@Test public void testFindLeftmostElements2() { BinaryTree level21 = new BinaryTree(null, null, 21); BinaryTree level22 = new BinaryTree(null, null, 22); BinaryTree level23 = new BinaryTree(null, null, 23); BinaryTree level24 = new BinaryTree(null, null, 24); BinaryTree level11 = new BinaryTree(level21, level22, 11); BinaryTree level12 = new BinaryTree(level23, level24, 12); BinaryTree root = new BinaryTree(level11, level12, 1); List<Integer> actual = findLeftmostElements(root); List<Integer> expected = Arrays.asList(1, 11, 21); assertThat(actual, is(expected)); }
@Test public void testFindLeftmostElements3() { BinaryTree level32 = new BinaryTree(null, null, 32); BinaryTree level34 = new BinaryTree(null, null, 34); BinaryTree level21 = new BinaryTree(null, null, 21); BinaryTree level22 = new BinaryTree(level34, null, 22); BinaryTree level23 = new BinaryTree(null, null, 23); BinaryTree level24 = new BinaryTree(level32, null, 24); BinaryTree level11 = new BinaryTree(level21, level22, 11); BinaryTree level12 = new BinaryTree(level23, level24, 12); BinaryTree root = new BinaryTree(level11, level12, 1); List<Integer> actual = findLeftmostElements(root); List<Integer> expected = Arrays.asList(1, 11, 21, 34); assertThat(actual, is(expected)); }