Simplest C++ Recursive Solution, 6 ms


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

Log in to reply
 

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