2021/05/05
二叉树的层次遍历,一遍过。
class Solution {
public List<Integer> rightSideView(TreeNode root) {
if (root == null) return Collections.emptyList();
LinkedList<TreeNode> q = new LinkedList<>();
List<Integer> res = new ArrayList<>();
q.offer(root);
while (!q.isEmpty()) {
int size = q.size();
Integer lastNode = null;
for (int i = 0; i < size; i++) {
TreeNode t = q.poll();
lastNode = t.val;
if (t.left != null) q.offer(t.left);
if (t.right != null) q.offer(t.right);
}
res.add(lastNode);
}
return res;
}
}