Clear C++ 5 lines DFS solution


  • 0
        int DFS(TreeNode* root, int sum,int count) {
            if(root==NULL) return 0;
            if(sum-root->val==0) count++;
            return count+DFS(root->left,sum-root->val,0)+DFS(root->right,sum-root->val,0);
        }
        
        int pathSum(TreeNode* root, int sum) {
            if(root==NULL) return 0;
            return DFS(root,sum,0)+pathSum(root->left,sum)+pathSum(root->right,sum);
        }
    

Log in to reply
 

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