二叉树的层序遍历是BFS算法,需要注意的点已经在代码中标出.
public class LevelOrder {
public List<List<Integer>> levelOrder(TreeNode root) {
List<List<Integer>> list = new ArrayList<>();
if (root == null) {
return list;
}
//java建议使用Deque来代替栈的实现
Deque<TreeNode> deque = new ArrayDeque<>();
deque.offer(root);
while (deque.size() > 0) {
int size = deque.size();
List<Integer> temp = new ArrayList<>();
while (size > 0) {
TreeNode curNode = deque.poll();
temp.add(curNode.val);
size--;//mark
if (curNode.left != null) {
deque.offer(curNode.left);
}
if (curNode.right != null) {
deque.offer(curNode.right);
}
}
list.add(temp);
}
return list;
}
public class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode(int x) {
val = x;
}
}
}