LeetCode404. 左叶子之和

class Solution {
public:
    int sum = 0;
    int sumOfLeftLeaves(TreeNode* root) {
        myDfs(root, 0);
        return sum;
    }

    void myDfs(TreeNode* root, int isLeft)
    {
        if (root == NULL)
        {
            return;
        }

        if (isLeft && root->left == NULL && root->right == NULL)
        {
            sum += root->val;
            return;
        }

        myDfs(root->left, 1);
        myDfs(root->right, 0);
    }
};

你可能感兴趣的:(算法)