404. Sum of Left Leaves

/**
 * 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, bool isLeft = false) {
        int res = 0;
        if (root == NULL)   return res;
        if (root->left == NULL && root->right == NULL && isLeft)
            return root->val;
        res += sumOfLeftLeaves(root->left, true);
        res += sumOfLeftLeaves(root->right, false);
        return res;
    }
};

猜你喜欢

转载自www.cnblogs.com/JTechRoad/p/9092955.html