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 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 path1->2
represents the number12
.
The root-to-leaf path1->3
represents the number13
.
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 path4->9->5
represents the number 495.
The root-to-leaf path4->9->1
represents the number 491.
The root-to-leaf path4->0
represents the number 40.
Therefore, sum = 495 + 491 + 40 =1026
.
[暴力解法]:
时间分析:
空间分析:
[优化后]:
时间分析:
空间分析:
[奇葩输出条件]:
[奇葩corner case]:
[思维问题]:
dfs的参数写错:sum由于经常要操作 而且需要返回,所以放在里面不用拿出来。
左右dfs的前提是root.l/r非空,空了就返回。所以空不空是一个重要的判断条件。
[英文数据结构或算法,为什么不用别的数据结构或算法]:
[一句话思路]:
[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):
[画图]:
[一刷]:
- 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路径求和的更多相关文章
- 【LeetCode】129. Sum Root to Leaf Numbers 解题报告(Python)
[LeetCode]129. Sum Root to Leaf Numbers 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/pr ...
- 【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 ...
- [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 ...
- 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 ...
- [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 ...
- 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 ...
- [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 ...
- 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 ...
- 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 ...
随机推荐
- android ListView 可缩放,支持左右上下手势
public class ZoomListView extends ListView implements SwipeListener { public static enum Action { Le ...
- 无状态http协议上用户的身份认证
1.注册时可以使用手机短信验证码进行身份认证 2.用户每次请求不能每次都发送验证码,这时需要服务器给客户端颁发一个身份凭证(一般为一个唯一的随机数),用户每次请求时都携带身份凭证, 服务器会记录该身份 ...
- alias重命名命令
升级了openssh后,发现ctrl+l忽然无法清屏了. 如果需要清屏的话,就得执行clear,但我更喜欢简单粗暴的做法,于是想起alias命令 方式一: 如果只想在当前终端生效(exit该窗口终端后 ...
- Javascript 蛤蟆可以吃队友,也可以吃对手 比较字符串
Javascript 蛤蟆可以吃队友,也可以吃对手 比较字符串 function mutation(arr) { for(var i = 0; i < arr[1].length; i++) { ...
- 11g R2 RAC 虚拟机
虚拟机安装RAC文档 本文档包含内容 一:安装系统 二:各节点配置系统参数 三:虚拟机创建共享存储 四:配置磁盘绑定 五:安装GRID 六:创建ASM DG 七:安装database 八:安装碰到的问 ...
- TreeSet的两种排序方式,含Comparable、Comparator
1.排序的引入 由于TreeSet可以实现对元素按照某种规则进行排序,例如下面的例子 public class TreeSetDemo { public static void main(String ...
- [ZZ] 麻省理工( MIT)大神解说数学体系
麻省理工( MIT)大神解说数学体系 http://blog.sina.com.cn/s/blog_5ff4fb7b0102e3p6.html 其实每一门学科都应该在学习完成后,在脑子里面有一个体系, ...
- 把java程序作为windows服务运行
参考: https://www.jianshu.com/p/fc9e4ea61e13 https://blog.csdn.net/qq_28566071/article/details/8088250 ...
- 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,截图如 ...
- processjs Documentation
Documentation Paul Nieuwelaar edited this page on 20 Sep 2017 · 4 revisions Installation & Usa ...