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 ...
随机推荐
- mongoose 数据库操作 - 分页
使用mongoose 加入分页方法,临时还没发现什么更好的方法,我使用的方法是,直接在源代码中加入 找到 node_modules/mongoose/lib/model.js打开这个文件.里面加入这段 ...
- debian支持ll命令
debian支持ll命令 $ ll -bash: ll: command not found 没有ll这个命令.尽管也知道ll事实上 是ls -l 这个命令的别名,可是总感觉不是非常习惯.由于之前一直 ...
- shell中判断用法
测试结构: 测试命令可用于测试表达式条件的真假,true,则返回0,false,则返回非0:这一点c/c++有区别: 格式: test expression #expression是一个 ...
- Python中的循环与跳出
--start-- for循环: for i in range(3): user_input = input("Your username:") passwd = int(inpu ...
- 用css实现列表菜单的效果
<!doctype html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- 分享非常有用的Java程序 (关键代码) (三)---创建ZIP和JAR文件
原文:分享非常有用的Java程序 (关键代码) (三)---创建ZIP和JAR文件 import java.util.zip.*; import java.io.*; public class Zip ...
- VirtualBox安装及使用说明和虚拟机安装XP系统图文教程
virtualbox是一款开源的虚拟机软件,它能够支持多种操作系统的安装如:Solaris.Windows.DOS.Linux.OS/2 Warp.BSD等系统作为client操作系统,而且最新版本号 ...
- C# 未能加载文件或程序集“MySQLDriverCS..." 错误解决
在解决方案的属性里,生成,里面有个目标平台,网上说的 大概也就是64位和32位的不兼容问题..试着把目标平台改为X86后竟然神奇的正常了!
- SEO分享:我为什么会有这么多的优质外链资源?
前面小浪发了一篇文章" [完整版]我是怎样3个月把800指数的词做上首页的.",非常多人看了之后都表示非常佩服.顽强的运行力.确实SEO就是要顽强的运行力,也有人说吹牛吧,一天50 ...
- 操作系统栈溢出检測之ucosII篇
操作系统栈溢出检測之uc/osII篇 Author : David Lin (林鹏) E-mail : linpeng1 ...