/**
* struct TreeNode {
* int val;
* struct TreeNode *left;
* struct TreeNode *right;
* };
*/
class Solution {
public:
/**
*
* @param root TreeNode类
* @return int整型
*/
/*
遍历所有的路径,然后记录下来,使用dfs
1,遍历所有的路径
2记录路径
*/
int dfs(TreeNode* root, int sum ) {
if (!root) {
return 0;
}
int total = sum * 10 + root->val;
if (!root->left && !root->right) {
return total;
} else {
return dfs(root->left, total) + dfs(root->right, total);
}
}
int sumNumbers(TreeNode* root) {
if (!root) {
return 0;
}
return dfs(root, 0);
}
};