Simple reursion in Java beats 80%


  • 0
    L
    public List<Integer> rightSideView(TreeNode root) {
            List<Integer> list = new ArrayList<>();
            helper(root,list,0);
            return list;
        }
        
        private void helper(TreeNode root, List<Integer> list,int level) {
            if(root==null) {
                return;
            }
            if(!(level<list.size())) {
                list.add(root.val);
            }
            helper(root.right,list,level+1);
            helper(root.left,list,level+1);
        }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.