Recursive 4 line solution in java


  • 0
    public int sumOfLeftLeaves(TreeNode root) {
        return root == null ? 0 : helper(root.left, true) + helper(root.right, false);
    }
    
    public int helper(TreeNode root, boolean isLeft) {
        if (root == null) return 0;
        if (root.left == null && root.right == null && isLeft) return root.val;
        else 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.