760 · 二叉树的右视图
方法1:BFS
public List<Integer> rightSideView(TreeNode root) {
List<Integer> res = new ArrayList<>();
if(root == null) return res;
Queue<TreeNode> q = new LinkedList<>();
q.offer(root);
while (!q.isEmpty()) {
int size = q.size();
for (int i = 0; i < size; i++) {
TreeNode cur = q.poll();
if(i==0) res.add(cur.val);
if(cur.right!=null) q.offer(cur.right);
if(cur.left!=null) q.offer(cur.left);
}
}
return res;
}