剑指Offer_编程题_从上往下打印二叉树
剑指Offer_编程题_从上往下打印二叉树
考点: 举例让抽象具体化
题目描述
从上往下打印出二叉树的每个节点,同层节点从左至右打印。
思路
用arraylist模拟一个队列来存储相应的TreeNodeimport java.util.ArrayList; /** 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<>(); ArrayList<TreeNode> queue = new ArrayList<>(); if (root == null) { return list; } queue.add(root); while (queue.size() != 0) { TreeNode temp = queue.remove(0); if (temp.left != null){ queue.add(temp.left); } if (temp.right != null) { queue.add(temp.right); } list.add(temp.val); } return list; } }