404. Sum of Left Leaves

class Solution {
public:
    int sumOfLeftLeaves(TreeNode* root) {
        if (!root) return 0;
        if (root->left && !root->left->left && !root->left->right) return root->left->val + sumOfLeftLeaves(root->right);//注意要保证是左节点, 节点的左右子都应该为空
        return sumOfLeftLeaves(root->left) + sumOfLeftLeaves(root->right);
    }
};

results matching ""

    No results matching ""