原题题目
代码实现(首刷看解)
/*** Definition for a binary tree node.* struct TreeNode {* int val;* struct TreeNode *left;* struct TreeNode *right;* };*/int sum;void calculate(struct TreeNode* root,int num)
{
num = (num << 1) + root->val;if(!root->left && !root->right)sum += num;if(root->left)calculate(root->left,num);if(root->right)calculate(root->right,num);
}int sumRootToLeaf(struct TreeNode* root){
if(!root)return 0;sum = 0;calculate(root,0);return sum;
}