import java.util.ArrayList;
import java.util.*;
/**
public class TreeNode {
int val = 0;
TreeNode left = null;
TreeNode right = null;
public TreeNode(int val) {
this.val = val;
}
}
*/
public class Solution {
// Queue<String> queue = new LinkedList<String>();
public ArrayList<Integer> PrintFromTopToBottom(TreeNode root) {
// Queue<TreeNode> queue = new LinkedList<>();
Queue<TreeNode> queue = new LinkedList<>();
ArrayList<Integer> list = new ArrayList<>();
if(root==null){
return list;
}
queue.offer(root);
while(!queue.isEmpty()){
// TreeNode node = queue.peek();
TreeNode node = queue.poll();
if(node.left!=null){
queue.offer(node.left);
}
if(node.right!=null){
queue.offer(node.right);
}
list.add(node.val);
}
return list;
}
}
算法思想:用队列的方式,入队根节点,然后将节点的左节点和有节点入队