您的位置:首页 > 其它

129. Sum Root to Leaf Numbers

2016-04-05 11:30 232 查看
/**
* 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:
void get_sum(TreeNode* root,int sum,int &result)
{
if(!root->left&&!root->right)
{
result+=(sum*10+root->val);
return;
}
if(root->left) get_sum(root->left,sum*10+root->val,result);
if(root->right) get_sum(root->right,sum*10+root->val,result);
}
int sumNumbers(TreeNode* root) {
if(!root) return 0;
int n=0;
get_sum(root,0,n);
return n;
}
};
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: