《从上往下打印二叉树》这道题为什么这样的代码不能通过?
public static ArrayListPrintFromTopToBottom(TreeNode root)
{
ArrayList array = new ArrayList();
if (root == null)
return null;
Queue nodes = new LinkedList();
nodes.offer(root);
while (nodes.size() > 0) {
TreeNode pnode = nodes.poll();
array.add(pnode.val);
if (pnode.left != null)
nodes.offer(pnode.left);
if (pnode.right != null)
nodes.offer(pnode.right);
}
if (array.isEmpty()) {
return null;
} else {
return array;
}
}
#Java工程师##算法工程师#