层次遍历二叉树
import java.util.ArrayList; import java.util.List; import java.util.Queue; import java.util.concurrent.LinkedBlockingDeque; /** 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) { List<Integer> list = new ArrayList<Integer>(); if(root==null) return (ArrayList<Integer>) list; Queue<TreeNode> queue = new LinkedBlockingDeque<TreeNode>(); queue.add(root); while(queue.size()>0){ TreeNode node05 = queue.poll(); list.add(node05.val); if(node05.left!=null) queue.add(node05.left); if(node05.right!=null) queue.add(node05.right); } return (ArrayList<Integer>) list; } }