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 ...
随机推荐
- 【转】介绍几个图论和复杂网络的程序库 —— BGL,QuickGraph,igraph和NetworkX
原文来自:http://blog.sciencenet.cn/blog-404069-297233.html 作复杂网络研究离不开对各种实际或模拟网络的统计.计算.绘图等工作.对于一般性的工作,我们可 ...
- 基于visual Studio2013解决C语言竞赛题之0523魔方阵
题目
- 什么是TimeTunnel
index - Taocode 欢迎使用TimeTunnel PageOutline(1-3,,inline) 什么是TimeTunnel !TimeTunnel(简称TT)是一个基于thrift通讯 ...
- Swift编程语言学习11—— 枚举全局变量、局部变量与类型属性
全局变量和局部变量 计算属性和属性监视器所描写叙述的模式也能够用于全局变量和局部变量,全局变量是在函数.方法.闭包或不论什么类型之外定义的变量,局部变量是在函数.方法或闭包内部定义的变量. 前面章节提 ...
- DHCP的工作原理
什么是dhcp?它是如何实现的? DHCP称为动态主机配置协议.DHCP服务允许工作站连接到网络并且自动获取一个IP地址.配置DHCP服务的服务器可以为每一个网络客户提供一个IP地址.子网掩码.缺省网 ...
- 演练2-1:创建MVC默认项目
在VS2012中点击“文件 | 新项目”,在弹出对话框中选择“Visual C# | Web | ASP.NET MVC 4 Web应用程序”. 在弹出的模板对话框中选择“Internet应用程序”和 ...
- list和用vector区别(Vector相当于是数组,读写快,插入慢)
stl提供了三个最基本的容器:vector,list,deque. vector和built-in数组类似,它拥有一段连续的内存空间,并且起始地址不变,因此它能非常好的支持随即存取,即[]操作符,但由 ...
- 从头学Qt Quick系列
http://www.cnblogs.com/csulennon/category/686605.html
- Javascript面向对象研究心得
这段时间正好公司项目须要,须要改动fullcalendar日历插件,有机会深入插件源代码.正好利用这个机会,我也大致学习了下面JS的面向对象编程,感觉收获还是比較多的. 所以写了以下这篇文章希望跟大家 ...
- linux服务之NFS和SAMBA服务
这几种网络文件传输最适合局域网.网络中用FTP 一:NFS服务 nfs(network file system)网络文件系统,改服务依赖于rpcbind服务.client通过rpc訪问server端的 ...