In the provided solution we skip nodes that are leafs when doing the level order traversal. How is that correct? What if that node is not only a leaf but also a node on the left side view then we will have an incorrect result. Consider this case as an example:
TreeNode root = new TreeNode(12);
root.left = new TreeNode(7);
root.right = new TreeNode(1);
root.left.left = new TreeNode(4);
root.left.right = new TreeNode(3);
root.left.right.left = new TreeNode(15);
root.left.right.left.left = new TreeNode(21);
When running the official solution this will give:
12 7 15 4 21 1 3
What would be expected is:
12 7 4 15 21 3 1