leetcode Sum Root to Leaf Numbers(所有路径之和)
观察题目给的返回值类型是int,可以断定这棵树的高度不会超过10,所以数据量其实是非常小的。那就直接dfs遍历这棵树,然后到叶子节点的时候将值加到最终结果上就OK了。思路非常之简单就不详述了。直接上代码:
class Solution {
public:
int sumNumbers(TreeNode *root) {
if(root==NULL)
return 0;
int total = 0;
dfs(root,0,total);
return total;
}
void dfs(TreeNode *root,int sum,int& total){
if(root==NULL)
return;
if(root->left==NULL&&root->right==NULL){
total += sum*10+root->val;
}else{..
dfs(root->left,sum*10+root->val,total);
dfs(root->right,sum*10+root->val,total);
}
}
};
leetcode Sum Root to Leaf Numbers(所有路径之和)的更多相关文章
- LeetCode: Sum Root to Leaf Numbers 解题报告
Sum Root to Leaf Numbers Given a binary tree containing digits from 0-9 only, each root-to-leaf path ...
- [LeetCode] Sum Root to Leaf Numbers 求根到叶节点数字之和
Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a number ...
- [leetcode]Sum Root to Leaf Numbers @ Python
原题地址:http://oj.leetcode.com/problems/sum-root-to-leaf-numbers/ 题意: Given a binary tree containing di ...
- LeetCode: Sum Root to Leaf Numbers [129]
[题目] Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a n ...
- 129. Sum Root to Leaf Numbers pathsum路径求和
[抄题]: Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a ...
- [Leetcode] Sum root to leaf numbers求根到叶节点的数字之和
Given a binary tree containing digits from0-9only, each root-to-leaf path could represent a number. ...
- LeetCode :: Sum Root to Leaf Numbers [tree、dfs]
Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a number ...
- Leetcode Sum Root to Leaf Numbers
Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a number ...
- [LeetCode] Sum Root to Leaf Numbers dfs,深度搜索
Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a number ...
随机推荐
- Git 配置editor编辑器
Git 配置editor编辑器 在ubuntu系统下,Git默认的编辑器是命令行,学名叫V什么的,使用起来诸多不便 在编辑提交日志的时候,用的比较多. 可以选择unbuntu默认的文档编辑器作为git ...
- WPF中StringFormat 格式化 的用法
原文 WPF中StringFormat 格式化 的用法 网格用法 <my:DataGridTextColumn x:Name="PerformedDate" Header=& ...
- Android Support Library更新到v22.1之AppCompat新特性
构建一个可以跑在不同版本 Android 平台的软件,是非常复杂和耗时的.为了解决这个问题,Android 推出了 Android Support Library (安卓兼容包),让新的UI控件也可以 ...
- NOI2015 寿司晚宴
今年NOI确实是在下输了.最近想把当时不会做的题都写一下. 题意 从2到n(500)这些数字中,选若干分给A,若干分给B,满足不存在:A的某个数和B的某个数的GCD不等于1. 对于寿司晚宴这题,标准解 ...
- 蓝牙Profile的概念和常见种类
蓝牙Profile Bluetooth的一个很重要特性,就是所有的Bluetooth产品都无须实现全部 的Bluetooth规范.为了更容易的保持Bluetooth设备之间的兼容,Bluetooth规 ...
- Python 第七篇:socket编程
一:socket基础: 1.1:Socket基础: socket起源于Unix,而Unix/Linux基本哲学之一就是“一切皆文件”,对于文件用[打开][读写][关闭]模式来操作.socket就是该模 ...
- initialize和init区别
Objective-C很有趣的一个地方是,它非常非常像C.实际上,它就是C语言加上一些其他扩展和一个运行时间(runtime). 有了这个在每个Objective-C程序中都会起作用的附加运行时间,给 ...
- 2个NASM开发环境下载以及30篇教程(lostspeed)
http://download.csdn.net/detail/lostspeed/8958175http://download.csdn.net/detail/lostspeed/8954263 h ...
- ModelAndView
我给你改一下public ModelAndView showDept(HttpServletRequest req,HttpServletResponse resp,ModelMap model){ ...
- SFTP上传下载(C#)
sftp是ftp协议的升级版本,是牺牲上传速度为代价,换取安全性能,本人开始尝试使用Tamir.SharpSSH.dll但它对新版本的openssh 不支持,所有采用Ssh.Net方式 需要依赖:Re ...