[抄题]:

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.

Note: A leaf is a node with no children.

Example:

Input: [1,2,3]
1
/ \
2 3
Output: 25
Explanation:
The root-to-leaf path 1->2 represents the number 12.
The root-to-leaf path 1->3 represents the number 13.
Therefore, sum = 12 + 13 = 25.

Example 2:

Input: [4,9,0,5,1]
4
/ \
9 0
 / \
5 1
Output: 1026
Explanation:
The root-to-leaf path 4->9->5 represents the number 495.
The root-to-leaf path 4->9->1 represents the number 491.
The root-to-leaf path 4->0 represents the number 40.
Therefore, sum = 495 + 491 + 40 = 1026.

[暴力解法]:

时间分析:

空间分析:

[优化后]:

时间分析:

空间分析:

[奇葩输出条件]:

[奇葩corner case]:

[思维问题]:

dfs的参数写错:sum由于经常要操作 而且需要返回,所以放在里面不用拿出来。

左右dfs的前提是root.l/r非空,空了就返回。所以空不空是一个重要的判断条件。

[英文数据结构或算法,为什么不用别的数据结构或算法]:

[一句话思路]:

[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):

[画图]:

[一刷]:

  1. sum = 0必须写在dfs里,每次重置为0。不然每次dfs会出现重复加的毛病。

[二刷]:

[三刷]:

[四刷]:

[五刷]:

[五分钟肉眼debug的结果]:

[总结]:

sum由于经常要操作 而且需要返回,所以放在里面不用拿出来。

[复杂度]:Time complexity: O(n) Space complexity: O(n)

[算法思想:迭代/递归/分治/贪心]:

[关键模板化代码]:

[其他解法]:

[Follow Up]:

[LC给出的题目变变变]:

[代码风格] :

[是否头一次写此类driver funcion的代码] :

[潜台词] :

class Solution {
public int sumNumbers(TreeNode root) {
//corner case
if (root == null) return 0;
//return
return dfs(root, 0);
} public int dfs(TreeNode root, int cur) {
//exit if left and right are null
if (root.left == null && root.right == null) return cur * 10 + root.val; //if not null, go left / right
int sum = 0;
if (root.left != null) sum += dfs(root.left, cur * 10 + root.val);
if (root.right != null) sum += dfs(root.right, cur * 10 + root.val); //return
return sum;
}
}

129. Sum Root to Leaf Numbers pathsum路径求和的更多相关文章

  1. 【LeetCode】129. Sum Root to Leaf Numbers 解题报告(Python)

    [LeetCode]129. Sum Root to Leaf Numbers 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/pr ...

  2. 【LeetCode】129. Sum Root to Leaf Numbers (2 solutions)

    Sum Root to Leaf Numbers Given a binary tree containing digits from 0-9 only, each root-to-leaf path ...

  3. [LeetCode] 129. Sum Root to Leaf Numbers 求根到叶节点数字之和

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

  4. 129. Sum Root to Leaf Numbers

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

  5. [LeetCode] 129. 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 OJ 129. Sum Root to Leaf Numbers

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

  7. [LC] 129. Sum Root to Leaf Numbers

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

  8. leetcode 129. Sum Root to Leaf Numbers ----- java

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

  9. leetcode@ [129] Sum Root to Leaf Numbers (DFS)

    https://leetcode.com/problems/sum-root-to-leaf-numbers/ Given a binary tree containing digits from 0 ...

随机推荐

  1. Redis集群配置(linux)

     *弄了一天,有问题直接问我.qq:137416943   1.redis集群的配置和简单使用   Redis集群配置 0.首先要配置环境: 0.1 安装c++ yum install gcc-c++ ...

  2. 戴尔R710服务器安装系统——配置raid

    一,内存二,硬盘(分区,数据量大小)三,电源线,网络线四,raid(raid0,raid1,raid5) 从这里开始 1.进入系统时不用管,默认进入即可 2.在读完内存消息之后,开始读取磁盘消息,在出 ...

  3. HTTP进阶学习笔记

    代理 HTTP的代理服务器既是Web服务器,又是Web客户端.使用代理可以"接触"到所有流过的HTTP流量,代理可以对其进行监视和修改.常见的就是对儿童过滤一些"成人&q ...

  4. jQuery基础(二)DOM

    DOM节点的创建 jQuery节点创建与属性的处理 创建元素节点: $("<div></div>") 创建为文本节点: $("<div> ...

  5. laravel 路由分組

    laravel 路由分組 Route::group(['prefix' => 'admin'], function () { $namespacePrefix="\\App\\Http ...

  6. Python 模块collections

    1.深入理解python中的tuple的功能 基本特性 # 可迭代 name_tuple = ('0bug', '1bug', '2bug') for name in name_tuple: prin ...

  7. dll文件32位64位检测工具以及Windows文件夹SysWow64的坑【转发】

    原文地址:http://www.cnblogs.com/hbccdf/archive/2014/03/09/3590916.html 自从操作系统升级到64位以后,就要不断的需要面对32位.64位的问 ...

  8. 小程序客服下发消息禁止后 session from 还有用吗?

    文章概要 1. 小程序下发政策调整分析 2. session from 数据还传到底三方了没?  1. 小程序下发政策调整分析 小程序客服功能下发策略调整                       ...

  9. Cannot change version of project facet Dynamic Web Module to 2.4问题解决

    问题现象: eclipse中,有个maven web项目,报错:Cannot change version of project facet Dynamic Web Module to 2.4,截图如 ...

  10. 解决mysqli的中文乱码问题

    有时候我们向服务器传汉字,出现内容为空,或???的问题,是因为mysqli对utf8的解码属于 Unicode码,会解析为 Unicode 码:所以我们要对内设置成gbk码, 尽管gbk码,很古来,但 ...