My C++ solution with Preorder traversal of 3ms


  • 0
    J

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

        if(!root) return sum;
        
        preOrder(root, sum);
        
        return sum;
    }
    
    void preOrder(TreeNode* root, int& sum) {
        if(root) {
            if(root->left != NULL && root->left->left == NULL && root->left->right == NULL)
                sum += root->left->val;
            preOrder(root->left, sum);
            preOrder(root->right, sum);
        }
    }
    

    };


Log in to reply
 

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