时间复杂度 ,空间复杂度 [平均空间复杂度 ]
class Solution {
int sol(TreeNode *rt, int dep) {
if(rt -> left == NULL and rt -> right == NULL) return rt -> val * dep;
int ans = 0;
if(rt -> left != NULL) ans += sol(rt -> left, dep + 1);
if(rt -> right != NULL) ans += sol(rt -> right, dep + 1);
return ans;
}
public:
int pathSum(TreeNode* root) {
return sol(root, 0);
}
};