题目:
从上往下打印出二叉树的每个节点,同层节点从左至右打印。
public class Solution { ArrayList<Integer> list = new ArrayList<Integer>();
public ArrayList<Integer> PrintFromTopToBottom(TreeNode root) {
if(root==null)
return list;
TreeNode node = root;
Queue<TreeNode> q = new LinkedList<TreeNode>();
q.offer(node);
while(!q.isEmpty()){
node = q.poll();
if(node.left!=null)
q.offer(node.left);
if(node.right!=null)
q.offer(node.right);
list.add(node.val);
}
return list;
}
}