<> Title Description

Print each node of the binary tree from top to bottom , Nodes on the same layer are printed from left to right .

<> Problem solving
import java.util.ArrayList; import java.util.Queue; import java.util.LinkedList
; /** public class TreeNode { int val = 0; TreeNode left = null; TreeNode right
= null; public TreeNode(int val) { this.val = val; } } */ public class Solution
{ public ArrayList<Integer> PrintFromTopToBottom(TreeNode root) { ArrayList<
Integer> list = new ArrayList<>(); if(root==null) return list; Queue<TreeNode>
queue= new LinkedList<>(); queue.offer(root); // Executed when the queue is not empty while (!queue.
isEmpty()){ // Take out the element at the top of the queue first list.add(queue.peek().val); // Take out the top element of the queue TreeNode poll
= queue.poll(); // if(poll.left!=null) queue.offer(poll.left); if(poll.right!=
null) queue.offer(poll.right); } return list; } }