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 ...
随机推荐
- 搞清楚学习Web的目的,是为了推广自己的产品和服务,不是为了替人接单做网页
只有这样,Web才不会沉沦于下流,才会对自己的事业有真正的帮助-
- 转:python中函数curry化
1 柯里化(Currying) 一个函数有多个参数,我们希望能固定其中几个参数的值. from functools import partial def foo(a,b,c): return a+b+ ...
- IBM Python 技术专题
Python 技术专题 Python 是由 Guido van Rossum 开发的,可免费获得的.是一种非常高级的解释型语言.其语法简单易懂,而且面向对象的语义功能强大又灵活,Python 可以广泛 ...
- UVA1366-----Martian Mining------DP
本文出自:http://blog.csdn.net/dr5459 题目地址: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&a ...
- Java之GUI编程(一)
GUI全称Graphical User Interfaces,意为图形用户户界面,又称为图形用户接口.GUI指的就是採用图形方式显示的计算机操作用户界面,打个例如吧.我们点击QQ图标,就会弹出一个QQ ...
- 平衡工作与生活的艺术——GTD简单介绍
1 开场白 大家好,今天是工作四年来第一次站在部门的分享会议上,所以有讲得不好的地方请大家见谅!而对于今天我想给大家介绍的"GTD工作方法",从2012年接触,认为对工作非常有帮助 ...
- arm:c语言和汇编混合编程
仅作演示. 1.C和汇编可相互调用,汇编子函数格式参考 汇编:普通的函数调用的汇编代码解析 http://www.cnblogs.com/mylinux/p/4139972.html 本文演示了 : ...
- 让你的 Qt 桌面程序看上去更加 native(一共六篇)
<让你的 Qt 桌面程序看上去更加 native>是一个系列文章.在这个系列中,你将会了解到如何让你的 Qt 桌面应用程序看上去更加 native.这里,我假设你已经清楚如何使用 Qt 编 ...
- shell中exec解析(转)
参考:<linux命令.编辑器与shell编程> <unix环境高级编程> exec和source都属于bash内部命令(builtins commands),在bash下输入 ...
- D16Pascal的编译器和IDE
https://github.com/Memnarch/D16Pascal https://github.com/Memnarch/D16IDE https://github.com/Memnarch ...