从上往下打印二叉树
从上往下打印出二叉树的每个节点,同层节点从左至右打印。
代码
public ArrayList<Integer> PrintFromTopToBottom(TreeNode root) {
ArrayList<Integer> list = new ArrayList<Integer>();
ArrayList<TreeNode> treelist = new ArrayList<TreeNode>();
treelist.add(root);
TreeNode temp ;
while(treelist.size()!=0){
temp = treelist.remove(0);
if(temp != null){
list.add(temp.val);
treelist.add(temp.left);
treelist.add(temp.right);
}
}
return list;
}