public List<Integer> rightSideView(TreeNode root) {
        if (root == null)
            return new ArrayList<>();

        Queue<TreeNode> queue = new LinkedList<>();
        queue.add(root);
        List<Integer> ret = new ArrayList<>();

        while (!queue.isEmpty()) {
            int size = queue.size();
            for (int i=0; i<size; i++) {
                TreeNode t = queue.remove();

                if (i==0)
                    ret.add(t.val);
                if (t.right != null)
                    queue.add(t.right);
                if (t.left != null)
                    queue.add(t.left);
            }
        }

        return ret;
    }