Java straightforward recursive solution


  • 0
    L
    /**
     * Definition for a binary tree node.
     * public class TreeNode {
     *     int val;
     *     TreeNode left;
     *     TreeNode right;
     *     TreeNode(int x) { val = x; }
     * }
     */
    public class Solution {
        public int sumOfLeftLeaves(TreeNode root) {
            if(root == null)
                return 0;
            int subSum = 0;
            if(root.left != null) 
                if(root.left.left == null && root.left.right == null) //determines if it is left leaf or not
                    subSum += root.left.val;
                
            subSum += sumOfLeftLeaves(root.left);
            subSum += sumOfLeftLeaves(root.right);
            
            return subSum;
        }
    }
    

Log in to reply
 

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