Sum Root to Leaf Numbers
2015-12-17 17:11
113 查看
Sum Root to Leaf Numbers
Total Accepted: 63531 Total Submissions: 202910 Difficulty: MediumGiven a binary tree containing digits from
0-9only, each root-to-leaf path could represent a number.
An example is the root-to-leaf path
1->2->3which represents the number
123.
Find the total sum of all root-to-leaf numbers.
For example,
1 / \ 2 3
The root-to-leaf path
1->2represents the number
12.
The root-to-leaf path
1->3represents the number
13.
Return the sum = 12 + 13 =
25.
(E) Path Sum (H) Binary Tree Maximum Path Sum
/** * 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: void sumNumbers(TreeNode* root,int &curSum,int num){ if(!root) return; num = num*10+root->val; if(root->left ==NULL && root->right ==NULL){ curSum += num; return ; } sumNumbers(root->left,curSum,num); sumNumbers(root->right,curSum,num); } int sumNumbers(TreeNode* root) { int sum = 0; sumNumbers(root,sum,0); return sum; } };
相关文章推荐
- CHTCollectionViewWaterfallLayout实现瀑布流
- 老李秘技:loadrunner11.5支持net4.0么?
- CocosBuilder使用总结
- AppServ + PhpStorm 搭建php开发环境
- 老李秘技:loadrunner11是否还支持dblib协议?
- TabActivity中某一个tab内切换activity页面的方法
- 用Eclipse tomcat创建一个新的Jsp页面
- Sybase修改interfaces文件(ip、端口、服务名等)
- 自定义控件步骤
- 每日一词【命令行CMD】
- iOS开发textfield的一些方法汇总
- 老李秘技:loadrunner回放脚本错误提示Error: "HTTP Status-Code 500"
- 老李说事:性能测试服务日记
- 第十二周项目4-(2)利用遍历思想求解图问题
- C++ 关于重载
- Linux学习笔记-2015-12-17
- css hack相关记录
- RoughSets属性约简算法
- storm spark 的选择区别
- 关于适配的应用