import java.util.ArrayList; import java.util.Collections; /** 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<TreeNode> queue = new ArrayList<TreeNode>(); ArrayList<Integer> list = new ArrayList<>(); if(root==null)return list; queue.add(root); while(!queue.isEmpty()) { TreeNode temp = queue.remove(0); list.add(temp.val); if(temp.left!=null)queue.add(temp.left); if(temp.right!=null)queue.add(temp.right); } return list; } }
原文:https://www.cnblogs.com/yihangZhou/p/10459138.html