public List<Integer> rightSideView(TreeNode root) {
        List<Integer> res = new LinkedList<Integer>();
        if (root == null)
            return res;
        Deque<TreeNode> queue = new LinkedList<TreeNode>();
        queue.add(root);
        while (!queue.isEmpty()) {
            int size = queue.size();
            res.add(queue.peekLast().val);
            while (size-- > 0) {
                TreeNode cur = queue.pollFirst();
                if (cur.left != null)
                    queue.offerLast(cur.left);
                if (cur.right != null)
                    queue.offerLast(cur.right);
            }
        }
        return res;
    }