1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
| public class Solution { public class TreeNode { int val = 0; TreeNode left = null; TreeNode right = null; public TreeNode(int val) { this.val = val; } }
public ArrayList<Integer> PrintFromTopToBottom(TreeNode root) { ArrayList<Integer> list = new ArrayList<>(); if(root==null){ return list; } ArrayList<TreeNode> up = new ArrayList<>(); up.add(root); while(!up.isEmpty()){ TreeNode temp = up.remove(0); if(temp.left!=null){ up.add(temp.left); } if(temp.right!=null){ up.add(temp.right); } list.add(temp.val); } return list; } }
|