C 代码
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* struct TreeNode *left;
* struct TreeNode *right;
* };
*/
int sum( struct TreeNode* root,int depth ){
if(!root) return 0;
if( !root->left && !root->right ) return root->val * depth;
return sum(root->left,depth+1) + sum(root->right,depth+1);
}
int pathSum(struct TreeNode* root) {
return sum(root,0);
}