2014 计算wpl
作者:
Lw1
,
2024-09-27 21:18:28
,
所有人可见
,
阅读 3
/**
* 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 sum(TreeNode *root , int h)
{
if(!root) return 0;
if(root->left == NULL && root->right == NULL)
{
return root->val * h;
}
int w_l , w_r;
w_l = sum(root->left , h + 1) , w_r = sum(root->right , h + 1);
return w_l + w_r;
}
int pathSum(TreeNode* root) {
return sum(root , 0);
}
};