Simple solution in Java


  • 0

    Just check if we are on the left and if the node is a leaf:

    public int sumOfLeftLeaves(TreeNode root) {
        if (root == null) {
            return 0;
        }
        int val = 0;
        if (root.left != null && isLeaf(root.left)) {
            val = root.left.val;
        }
        return val + sumOfLeftLeaves(root.left) + sumOfLeftLeaves(root.right);
    }
    
    private boolean isLeaf(TreeNode node) {
        return node.left == null && node.right == null;
    }
    

Log in to reply
 

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