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

An example is the root-to-leaf path 1->2->3 which represents
the number 123.

Find the total sum of all root-to-leaf numbers.

For example,

  1. 1
  2. / \
  3. 2 3

The root-to-leaf path 1->2 represents the number 12.

The root-to-leaf path 1->3 represents the number 13.

Return the sum = 12 + 13 = 25.

从根节点開始,dfs的思路,事实上也就是postorder(先序遍历),遍历路径上的值每次乘以基数10,过程非常easy,代码例如以下:

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

LeetCode :: Sum Root to Leaf Numbers [tree、dfs]的更多相关文章

  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求根到叶节点的数字之和

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

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

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

  7. 129. 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 dfs,深度搜索

    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)

    题意: 给一棵二叉树,每个节点上有一个数字,范围是0-9,将从根到叶子的所有数字作为一个串,求所有串的和. 思路: 普通常规的DFS. /** * Definition for a binary tr ...

随机推荐

  1. js-offsetX、pageX、clientX、layerX、screenX

    真心地我也是懵逼的 clientX,clientY:针对屏幕有效区域,不包括滚动部分,坐标(0,0)一直在有效区域的左上角 X,Y:            针对屏幕有效区域,不包括滚动部分,坐标(0, ...

  2. HDU 4920.Matrix multiplication-矩阵乘法

    Matrix multiplication Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/ ...

  3. (10)java基础知识-字符串

    String s和 new String 的区别 String s1="hello"; String s2= new String("hello"); Stri ...

  4. ORA-17003. Invalid column index

    sql里面有? 希望输入有参数 java程序里面没有给入参数

  5. POJ 3420 Quad Tiling (矩阵乘法)

    [题目链接] http://poj.org/problem?id=3420 [题目大意] 给出一个4*n的矩阵,求用1*2的骨牌填满有多少方案数 [题解] 弄出不同情况的继承关系,用矩阵递推即可. [ ...

  6. A folder failed to be renamed or moved--安装Android SDK的问题

    对于Android是一直想学却一直未学,行动跟不上想法.现在,终于付诸于行动了. 首先,我找的第一个Android的资料是大话企业级Android,前阵子刚看完大话设计模式,通俗易懂,还是比较喜欢这一 ...

  7. C# 窗体位置 Show和ShowDialog (转载)

    CenterParent                     窗体在其父窗体中居中.       CenterScreen                    窗体在当前显示窗口中居中,其尺寸在 ...

  8. mysql关于访问权限以及root密码修改

    root密码修改:mysql> use mysql;mysql> UPDATE user SET Password = PASSWORD('newpass') WHERE user = ' ...

  9. python实现scrapy爬取图片到本地时的sha1摘要算法文件名

    2017-03-29 Scrapy爬图片到本地应该会给图片自动生成sha1摘要算法文件名,我第一次用scrapy也不清楚太多,就在程序里自己写了一段实现这一功能的代码.需import hashlib ...

  10. ECSHOP筛选属性修改title标题

    发现百度蜘蛛爬行网站时会爬行属性链接,而且会进行收录.可是ecshop系统制作的网站,在分类页点击属性筛选出产品时,网页title不会改变.这样就会造成大量title一样的页面,不利于优化.为此,在网 ...