【LeetCode】左叶子之和

题目描述:

https://leetcode-cn.com/problems/sum-of-left-leaves/

代码:

递归

class Solution {
public:
    int sumOfLeftLeaves(TreeNode* root) {
        return SumOfLeft(root, false);
    }
    int SumOfLeft(TreeNode* root, bool flag)
    {
        if(!root)
            return 0;
        if(!root->left && !root->right && flag)
            return root->val;
        return SumOfLeft(root->left, true) + SumOfLeft(root->right, false);
    }
};

你可能感兴趣的:(【LeetCode】左叶子之和)