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;
    }
};

 

posted @ 2018-05-26 15:01  JTechRoad  阅读(78)  评论(0编辑  收藏  举报