从上到下打印二叉树
从上往下打印二叉树
http://www.nowcoder.com/questionTerminal/7fe2212963db4790b57431d9ed259701
//解题思路: //其实就是一个广度优先遍历而已 ArrayList<Integer> result = new ArrayList<>(); public ArrayList<Integer> PrintFromTopToBottom(TreeNode root) { if(root == null) { return result; } Queue<TreeNode> queue = new LinkedList<>(); queue.offer(root); while(!queue.isEmpty()) { TreeNode node = queue.poll(); result.add(node.val); if(node.left!=null) { queue.offer(node.left); } if(node.right!=null) { queue.offer(node.right); } } return result; }