Posted on

Description

Submission

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    int sumOfLeftLeaves(TreeNode* root) {
        if(!root) return 0;
        return sumOfLeftLeavesUtil(root->left, true) + 
            sumOfLeftLeavesUtil(root->right, false);
    }

private:
    int sumOfLeftLeavesUtil(TreeNode* root, bool isLeft) {
        if(!root) return 0;
        if(isLeft && !root->left && !root->right) 
        return root->val;
        return sumOfLeftLeavesUtil(root->left, true) + 
                sumOfLeftLeavesUtil(root->right, false);
    }   
};

Leave a Reply

Your email address will not be published. Required fields are marked *