The problem of this solution can be BFS and DFS.
BFS is easy to understand.
The following is DFS solution:
private List<Integer> res = new ArrayList<>(); public List<Integer> rightSideView(TreeNode root) { helper(root, 0); return res; } private void helper(TreeNode root, int level){ if(root==null) return; if(res.size()==level){ res.add(root.val); } helper(root.right, level+1); helper(root.left, level+1); }