s4553711
4/27/2017 - 3:06 PM

404.cpp

/**
 * 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 result;
    int sumOfLeftLeaves(TreeNode* root) {
        result = 0;
        traversal(root);
        return result;
    }
    
    void traversal(TreeNode* node) {
        if (node == NULL) return;
        if (node->left != NULL && node->left->left == NULL && node->left->right == NULL) {
            result += node->left->val;
        } else {
            traversal(node->left);
        }
        traversal(node->right);
    }
};