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