没啥难度,和之前一道记录每层的遍历差不多,这个就是取每层的末尾而已,做法都是一样的
// 冲刺020
class Solution {
public List<Integer> rightSideView(TreeNode root) {
final List<Integer> ans = new LinkedList<>();
Deque<TreeNode> deque = new LinkedList<>();
if(root == null) {
return ans;
}
deque.offer(root);
while (!deque.isEmpty()) {
int size = deque.size();
TreeNode node = null;
for (int i = 0; i < size; i++) {
node = deque.poll();
if (node.left != null) {
deque.offer(node.left);
}
if (node.right != null) {
deque.offer(node.right);
}
}
ans.add(node.val);
}
return ans;
}
}