8-line JAVA recursive solution


  • 0
    public int sumOfLeftLeaves(TreeNode root) {
        return helper(root,false);
    }
    private int helper(TreeNode root,boolean isLeftChild){
        if(root==null) return 0;
        if(isLeftChild&&root.left==null&&root.right==null) return root.val;
        return helper(root.left,true) + helper(root.right,false);
    }

Log in to reply
 

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