转载请注明来自souldak,微博:@evagle

观察题目给的返回值类型是int,可以断定这棵树的高度不会超过10,所以数据量其实是非常小的。那就直接dfs遍历这棵树,然后到叶子节点的时候将值加到最终结果上就OK了。思路非常之简单就不详述了。直接上代码:

  1. class Solution {
  2. public:
  3. int sumNumbers(TreeNode *root) {
  4. if(root==NULL)
  5. return 0;
  6. int total = 0;
  7. dfs(root,0,total);
  8. return total;
  9. }
  10. void dfs(TreeNode *root,int sum,int& total){
  11. if(root==NULL)
  12. return;
  13. if(root->left==NULL&&root->right==NULL){
  14. total += sum*10+root->val;
  15. }else{..
  16. dfs(root->left,sum*10+root->val,total);
  17. dfs(root->right,sum*10+root->val,total);
  18. }
  19. }
  20. };

leetcode Sum Root to Leaf Numbers(所有路径之和)的更多相关文章

  1. 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 ...

  2. [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 ...

  3. [leetcode]Sum Root to Leaf Numbers @ Python

    原题地址:http://oj.leetcode.com/problems/sum-root-to-leaf-numbers/ 题意: Given a binary tree containing di ...

  4. 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 ...

  5. 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 ...

  6. [Leetcode] Sum root to leaf numbers求根到叶节点的数字之和

    Given a binary tree containing digits from0-9only, each root-to-leaf path could represent a number. ...

  7. 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 ...

  8. 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 ...

  9. [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 ...

随机推荐

  1. mongoose 数据库操作 - 分页

    使用mongoose 加入分页方法,临时还没发现什么更好的方法,我使用的方法是,直接在源代码中加入 找到 node_modules/mongoose/lib/model.js打开这个文件.里面加入这段 ...

  2. debian支持ll命令

    debian支持ll命令 $ ll -bash: ll: command not found 没有ll这个命令.尽管也知道ll事实上 是ls -l 这个命令的别名,可是总感觉不是非常习惯.由于之前一直 ...

  3. shell中判断用法

    测试结构: 测试命令可用于测试表达式条件的真假,true,则返回0,false,则返回非0:这一点c/c++有区别:       格式: test  expression #expression是一个 ...

  4. Python中的循环与跳出

    --start-- for循环: for i in range(3): user_input = input("Your username:") passwd = int(inpu ...

  5. 用css实现列表菜单的效果

    <!doctype html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  6. 分享非常有用的Java程序 (关键代码) (三)---创建ZIP和JAR文件

    原文:分享非常有用的Java程序 (关键代码) (三)---创建ZIP和JAR文件 import java.util.zip.*; import java.io.*; public class Zip ...

  7. VirtualBox安装及使用说明和虚拟机安装XP系统图文教程

    virtualbox是一款开源的虚拟机软件,它能够支持多种操作系统的安装如:Solaris.Windows.DOS.Linux.OS/2 Warp.BSD等系统作为client操作系统,而且最新版本号 ...

  8. C# 未能加载文件或程序集“MySQLDriverCS..." 错误解决

    在解决方案的属性里,生成,里面有个目标平台,网上说的 大概也就是64位和32位的不兼容问题..试着把目标平台改为X86后竟然神奇的正常了!

  9. SEO分享:我为什么会有这么多的优质外链资源?

    前面小浪发了一篇文章" [完整版]我是怎样3个月把800指数的词做上首页的.",非常多人看了之后都表示非常佩服.顽强的运行力.确实SEO就是要顽强的运行力,也有人说吹牛吧,一天50 ...

  10. 操作系统栈溢出检測之ucosII篇

    操作系统栈溢出检測之uc/osII篇 Author               :       David Lin (林鹏) E-mail               :       linpeng1 ...