首页 > 其他 > 详细

Leetcode#129 Sum Root to Leaf Numbers

时间:2015-02-01 23:01:40      阅读:311      评论:0      收藏:0      [点我收藏+]

原题地址

 

二叉树的遍历

 

代码:

 1 vector<int> path;
 2     
 3 int sumNumbers(TreeNode *root) {
 4         if (!root)
 5             return 0;
 6             
 7         int sum = 0;
 8         path.push_back(root->val);
 9         
10         if (!root->left && !root->right) {
11             for (int i = 0; i < path.size(); i++)
12                 sum = sum * 10 + path[i];
13         }
14         else {
15             if (root->left)
16                 sum += sumNumbers(root->left);
17             if (root->right)
18                 sum += sumNumbers(root->right);
19         }
20         
21         path.pop_back();
22         
23         return sum;
24 }

 

Leetcode#129 Sum Root to Leaf Numbers

原文:http://www.cnblogs.com/boring09/p/4266200.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!