Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a number.

An example is the root-to-leaf path 1->2->3 which 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->2 represents the number 12.
The root-to-leaf path 1->3 represents the number 13.

Return the sum = 12 + 13 = 25.

Hide Tags

Tree Depth-first Search

/**
* Definition for binary tree
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
private:
int sum;
public:
void dfs(TreeNode *root,int num){
if(root==NULL)
return;
if(root->left==NULL && root->right==NULL){
num=num*+root->val;
sum=sum+num;
return;
}
num=num*+root->val;
dfs(root->left,num);
dfs(root->right,num);
}
int sumNumbers(TreeNode *root) {
sum=;
dfs(root,);
return sum; }
};

最新文章

  1. sql查询当天,一周,一个月数据的语句
  2. Java字节流:InputStream OutputStream
  3. solaris之复习
  4. Android开发代码规范(转)
  5. 【jQuery UI 1.8 The User Interface Library for jQuery】.学习笔记.2.更换主题
  6. 使用Parallel
  7. http 302
  8. JavaScript继承学习笔记
  9. 下拉刷新ListView实现原理
  10. JDBC高级部分
  11. JavaScript类数组对象参考
  12. Java中的回调函数学习
  13. CodeForces 614B Gena's Code
  14. 201521123014 《Java程序设计》第9周学习总结
  15. Java实现mongodb原生增删改查语句
  16. JS创建对象之原型模式
  17. 笨鸟先飞之ASP.NET MVC系列之过滤器(04认证过滤器)
  18. JQuery编写自己的插件(七)
  19. Delphi不注册COM直接使用ActiveX控件并绑定事件
  20. docker学习实践之路[第五站]mysql镜像应用

热门文章

  1. python urllib模块中的方法
  2. dubbo入门学习(六)-----dubbo原理
  3. JS控制视频的播放
  4. IO流 输入和输出文档内容
  5. angular7.X配置同时使用localhost和本机IP访问项目
  6. UnhandledPromiseRejectionWarning: SequelizeConnectionError: Client does not support authentication protocol requested by server; consider upgrading MySQL client
  7. linux apache vhost
  8. LintCode 链表倒数第n个节点
  9. 用CSS添加选中文字的背景色
  10. java list转换json格式