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.

  

/**
* Definition for binary tree
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
void solve(TreeNode *root)
{
if(root -> left == NULL && root ->right == NULL){
sum += root->val;
return;
}
if(root->left){
root->left->val += root->val * ;
solve(root->left);
} if(root->right){
root->right->val += root->val * ;
solve(root->right);
}
}
int sumNumbers(TreeNode *root) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
sum = ;
if(root == NULL) return sum;
solve(root);
return sum;
}
int sum ;
};

最新文章

  1. APUE学习之多线程编程(二):线程同步
  2. jQuery插件 -- 动态事件绑定插件jquery.livequery.js
  3. iOS中响应者链条-触摸事件
  4. 仓库、超市、服装、食品、批发零售手持打印PDA开单器-现场无线开单扫描 无线传输电脑
  5. Unit01-OOP-对象和类(上)
  6. kuangbin_ShortPath R (HDU 4370)
  7. 1.7.7 Spell Checking -拼写检查
  8. Linux 模拟 鼠标 键盘 事件
  9. PHPCMS v9构建模块 - 实例之企业服务模块
  10. codeblock 设置背景颜色
  11. hive 不同用户 权限设置 出错处理
  12. zf-关于调用页面提示找不到className的原因
  13. 任务定义器——SocketProcessor
  14. mac gulp: command not found
  15. 库存秒杀问题-redis解决方案- 接口限流
  16. 《DSP using MATLAB》Problem 7.11
  17. 放棋子|2012年蓝桥杯B组题解析第七题-fishers
  18. Remove K Digits
  19. 【IDEA】【maven】idea使用maven插件 打包提示找不到符号找不到类,但是却没有错误
  20. Linux数据库:MYSQL启用和查看二进制日志

热门文章

  1. Unity 3D中的界面快捷键
  2. 功率和dB的关系
  3. Powershell Switch 条件
  4. URAL 1029
  5. pthread_mutex_init & 互斥锁pthread_mutex_t的使用
  6. SOA 新业务语言 新系统架构——什么是SOA
  7. hdu5017:补题系列之西安网络赛1011
  8. hdu5014:number sequence对称思想
  9. chapter 2: Representing and manipulating information
  10. c语言局部变量 静态局部变量 全局变量与静态全局变量