1 /**
 2  * Definition for binary tree
 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 public:
12     void getSum(TreeNode *root, int &result, int sum) {
13         if (!root) return;
14         sum = sum*10 + root->val;
15         if (!root->left && !root->right) {
16             result += sum;
17             return;
18         }
19         getSum(root->left, result, sum);
20         getSum(root->right, result, sum);
21     }
22     int sumNumbers(TreeNode *root) {
23         int result = 0;
24         getSum(root, result, 0);
25         return result;
26     }
27 };

 

posted on 2015-03-24 15:45  keepshuatishuati  阅读(143)  评论(0编辑  收藏  举报