Easy Solution in C++


  • 0
    T
    class Solution {
    
        int sumOfLeaves(TreeNode* root, int l) 
        {
            //int l passed to check left(0) or right(1) child
    
            if(!root)
                return 0;
            if(!root->left && !root->right && l==0)
                return root->val;
            return sumOfLeaves(root->left,0) + sumOfLeaves(root->right,1);
        }
    
    public:
        int sumOfLeftLeaves(TreeNode* root) {
             if(!root)
                return 0;
            return sumOfLeaves(root->left,0) + sumOfLeaves(root->right,1);
        }
    };
    

Log in to reply
 

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