正常的dfs
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
int dfs(TreeNode* root,int cnt)
{
if (!root) return 0;
if (!root->left && !root->right) return cnt * root->val;
return dfs(root->left, cnt + 1) + dfs(root->right, cnt + 1);
}
int pathSum(TreeNode* root) {
return dfs(root,0);
}
};