题目
法1:层次遍历
最佳方法,牢记!!!
class Solution {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 tmpNode = q.poll();if (tmpNode.left != null) {q.offer(tmpNode.left);}if (tmpNode.right != null) {q.offer(tmpNode.right);}if (i == size - 1) {res.add(tmpNode.val);}}}return res;}
}