clueless coding // TODO: be smarter

LeetCode Sum of Left Leaves: Java Solution



TL;DR Code Solution



public int helper(TreeNode root, boolean isLeft) {

    if (root == null) {
        return 0;
    }

    if (root.left == null && root.right == null) {
        return isLeft ? root.val : 0;
    }

    return helper(root.left, true) + helper(root.right, false);


}

public int sumOfLeftLeaves(TreeNode root) {
    return helper(root, false);
}