Java 3-line Solution


  • 0
    Y
    public int sumOfLeftLeaves(TreeNode root) {
        if(root == null) return 0;
        if(root.left != null && root.left.left == null && root.left.right == null) return root.left.val + sumOfLeftLeaves(root.right);
        return sumOfLeftLeaves(root.left) + sumOfLeftLeaves(root.right);
    }
    

  • 0
    H

    One line solution:

    public int sumOfLeftLeaves(TreeNode root) {
            return root == null ? 0 : ((root.left != null && root.left.left == null && root.left.right == null) ? root.left.val+sumOfLeftLeaves(root.right) : sumOfLeftLeaves(root.left)+sumOfLeftLeaves(root.right));
        }
    

Log in to reply
 

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