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->2represents the number12.
The root-to-leaf path1->3represents 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->5represents the number 495.
The root-to-leaf path4->9->1represents the number 491.
The root-to-leaf path4->0represents 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 ...
随机推荐
- 【SpringBoot】数据库操作之整合Mybaties和事务讲解
========================8.数据库操作之整合Mybaties和事务讲解 ================================ 1.SpringBoot2.x持久化数 ...
- <a>标签里的函数事件写法的实战建议
如果在实际应用中确实是要用到标签来响应onclick事件的, 那么就建议使用下面三种方法 <a href="javascript:void(0);" onclick=&quo ...
- C++ std::async vs async/await in C# - Stack Overflow
C++ std::async vs async/await in C# - Stack Overflow 我想知道新的c ++功能std::async是否与两个C#关键字async / await相当 ...
- php最常见最经典的算法题
1.一群猴子排成一圈,按1,2,…,n依次编号.然后从第1只开始数,数到第m只,把它踢出圈,从它后面再开始数,再数到第m只,在把它踢出去…,如此不停的进行下去,直到最后只剩下一只猴子为止,那只猴子就叫 ...
- .NET WebService 入门
以 前写博客最主要的就是不知道写什么东西,现在感觉能写点东西,就是感觉博客随笔的标题挺难取的,最近工作中刚好用到了WebService,刚好可以写一 篇博客.去年工作的时候自己也用到过,只是知道调用一 ...
- 小程序 去掉 <button /> 组件默认 border 边框样式
代码: <button class="btn">点击</button> 效果图: 目的:去掉 <button /> 默认样式中的 backgro ...
- E212: Can't open file for writing Press ENTER or type command to continue
E212: Can't open file for writing Press ENTER or type command to continue 出现这个错误的原因可能有两个: 1.当前用户的权限不 ...
- docker stats top diff和cp
实战[root@localhost ~]# docker ps -aCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES1fb916fb0e13 ...
- Faster R-CNN代码例子
主要参考文章:1,从编程实现角度学习Faster R-CNN(附极简实现) 经常是做到一半发现收敛情况不理想,然后又回去看看这篇文章的细节. 另外两篇: 2,Faster R-CNN学习总结 ...
- Windows下,python pip安装时ReadTimeoutError解决办法
一般情况下PIP出现ReadTimeoutError都是因为被GFW给墙了,所以一般遇到这种问题,我们可以选择国内的镜像来解决问题. 在Windows下: C:\Users\Administrator ...