Sum Root to Leaf Numbers深度优先计算路径和
2024-10-08 01:51:15
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
/**
* 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; }
};
最新文章
- sql查询当天,一周,一个月数据的语句
- Java字节流:InputStream OutputStream
- solaris之复习
- Android开发代码规范(转)
- 【jQuery UI 1.8 The User Interface Library for jQuery】.学习笔记.2.更换主题
- 使用Parallel
- http 302
- JavaScript继承学习笔记
- 下拉刷新ListView实现原理
- JDBC高级部分
- JavaScript类数组对象参考
- Java中的回调函数学习
- CodeForces 614B Gena's Code
- 201521123014 《Java程序设计》第9周学习总结
- Java实现mongodb原生增删改查语句
- JS创建对象之原型模式
- 笨鸟先飞之ASP.NET MVC系列之过滤器(04认证过滤器)
- JQuery编写自己的插件(七)
- Delphi不注册COM直接使用ActiveX控件并绑定事件
- docker学习实践之路[第五站]mysql镜像应用
热门文章
- python urllib模块中的方法
- dubbo入门学习(六)-----dubbo原理
- JS控制视频的播放
- IO流 输入和输出文档内容
- angular7.X配置同时使用localhost和本机IP访问项目
- UnhandledPromiseRejectionWarning: SequelizeConnectionError: Client does not support authentication protocol requested by server; consider upgrading MySQL client
- linux apache vhost
- LintCode 链表倒数第n个节点
- 用CSS添加选中文字的背景色
- java list转换json格式