层序遍历
1.把根结点放到队列中
2.循环直到?
1.从队列取出队首元素 2.孩子入队列
public static void levelOrder1(TreeNode root) {
if (root == null) {
return;
}
Queue<TreeNode> queue = new LinkedList<>();
queue.add(root);
while (!queue.isEmpty()) {
TreeNode front = queue.poll();
System.out.println(front.val);
if (front.left != null) {
queue.add(front.left);
}
if (front.right != null) {
queue.add(front.right);
}
}
}