My Java recursive solution


  • 0

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

    public int sumOfLeftLeaves(TreeNode root, boolean fromLeft) {
        if (root==null) return 0; 
        if (root.left==null && root.right==null) return fromLeft ? root.val : 0;
        return sumOfLeftLeaves(root.left, true) + sumOfLeftLeaves(root.right, false);
    }
    

    }


Log in to reply
 

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