c++ dfs (O(n))


  • 0
    int sum(int n, TreeNode* node) {
        if (!node) return 0;
        if (n == 1 && !node->left && !node->right) {
            return node->val;
        }
        return sum(1, node->left) + sum(2, node->right);
    }
    int sumOfLeftLeaves(TreeNode* root) {
        return sum(0, root);
    }
    

Log in to reply
 

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