/**
* 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 sumNumbers(TreeNode* root)
{
return dfs(root,0);
}
int dfs(TreeNode* root,int sum)
{
if(root==NULL)
return 0;
if(root->left==NULL && root->right==NULL)
return 10*sum+root->val;
return dfs(root->left,10*sum+root->val)+dfs(root->right,10*sum+root->val);
}
};
Sum Root to Leaf Numbers
最新推荐文章于 2022-04-08 10:01:02 发布