Clean C++ Recursive Solution


  • 0
    S

    We should check if this is the left leave two levels above.

    class Solution {
    public:
        int sumOfLeftLeaves(TreeNode* root) {
            if ( root == nullptr ) return 0;
            int ans = 0;
            if ( root->left != nullptr && root->left->left == nullptr && root->left->right == nullptr) ans = root->left->val;
            return ans + sumOfLeftLeaves(root->left) + sumOfLeftLeaves(root->right);
        }
    };
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.