使用队列代码
从上往下打印二叉树
http://www.nowcoder.com/questionTerminal/7fe2212963db4790b57431d9ed259701
public class Solution { public ArrayList<Integer> PrintFromTopToBottom(TreeNode root) { LinkedList<TreeNode> queue = new LinkedList<>(); ArrayList<Integer> result = new ArrayList<>(); queue.add(root); while(!queue.isEmpty()) { TreeNode node = queue.poll(); if(node != null) { result.add(node.val); queue.add(node.left); queue.add(node.right); } } return result; } }