1 /** 2 * Definition for a binary tree node. 3 * struct TreeNode { 4 * int val; 5 * TreeNode *left; 6 * TreeNode *right; 7 * TreeNode(int x) : val(x), left(NULL), right(NULL) {} 8 * }; 9 */ 10 class Solution 11 { 12 public: 13 int helper(TreeNode* root, int sum) 14 { 15 if(!root) return 0; 16 if(!root->left && !root->right) return 10*sum + root->val; 17 return helper(root->left, 10*sum + root->val) + helper(root->right, 10*sum + root->val); 18 } 19 int sumNumbers(TreeNode* root) 20 { 21 return helper(root, 0); 22 } 23 };
原文地址:https://www.cnblogs.com/yuhong1103/p/12616659.html
时间: 2024-11-12 23:17:07