public class Solution {
public List<Integer> rightSideView(TreeNode root) {
List<Integer> res = new ArrayList<Integer>();
if(root == null) return res;
visitLevel(root, 1, res);
return res;
}
public void visitLevel(TreeNode root, int level, List<Integer> res){
if(root == null) return;
if(level > res.size()){
res.add(root.val);
}
visitLevel(root.right, level+1, res);
visitLevel(root.left, level+1, res);
}
}
Recursive solution


I thought this is C++, my stupid mistake, sorry!
No more problems.
Hope you're interested, here is my C++ solution in 7 ms (which uses almost identical algorithm):
https://leetcode.com/discuss/33191/18linerecursivecsolutionin7ms