Clean C++ Solution 3ms


  • 0
    V
    class Solution {
    public:
        int sumOfLeftLeaves(TreeNode* root) {
            if(root == nullptr){return 0;}
    
            return sumOfLeftLeavesUtil(root, false);
        }
        
        int sumOfLeftLeavesUtil(TreeNode* root, bool isLeft){
            if(root == nullptr){return 0;}
            
            // This is a leaf node return its value
            if(isLeft && !root->left && !root->right){
                return root->val;
            }
            
            return sumOfLeftLeavesUtil(root->left, true)
                + sumOfLeftLeavesUtil(root->right, false);
        }
    };
    

Log in to reply
 

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