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 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
/ \
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.
SOLUTION 1:
使用递归完成。我们要做的是 把左右子树的path加起来。
base case: root是叶子节点时,直接计算path。我们用pre保留上一层计算出来的数字的值。那么到了当前根节点应该就是
把上一层的值*10再加上root的值。
/**
* Definition for binary tree
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public int sumNumbers(TreeNode root) {
return dfs(root, 0);
} public int dfs(TreeNode root, int pre) {
if (root == null) {
return 0;
} int cur = pre * 10 + root.val;
if (root.left == null && root.right == null) {
return cur;
} return dfs(root.left, cur) + dfs(root.right, cur);
}
}
GITHUB:
https://github.com/yuzhangcmu/LeetCode_algorithm/blob/master/tree/SumNumbers_1208_2014.java
LeetCode: Sum Root to Leaf Numbers 解题报告的更多相关文章
- 【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 解题思路
Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a number ...
- [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 ...
- [leetcode]Sum Root to Leaf Numbers @ Python
原题地址:http://oj.leetcode.com/problems/sum-root-to-leaf-numbers/ 题意: Given a binary tree containing di ...
- 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 ...
- 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 ...
- [Leetcode] Sum root to leaf numbers求根到叶节点的数字之和
Given a binary tree containing digits from0-9only, each root-to-leaf path could represent a number. ...
- 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 ...
- [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 ...
随机推荐
- linux top命令查看内存及多核CPU的使用讲述【转】
转载一下top使用后详细的参数,之前做的笔记找不见了,转载一下,作为以后的使用参考: 原文地址:http://blog.csdn.net/linghao00/article/details/80592 ...
- 树莓派进阶之路 (026) - 基于 Samba 实现 NAS 系统
摆弄了几天Raspberry Pi,在搞定了无线网络.FTP服务之后,打算更进一步,通过Samba实现NAS系统与PC共享文件.需要安装的软件:sudo apt-get install samba s ...
- Ubuntu菜鸟入门(十二)—— 主题美化
一.unity-tweak-tool 1.软件介绍 调整 Unity 桌面环境,还是推荐使用Unity Tweak Tool,这是一个非常好用的 Unity 图形化管理工具,可以修改工作区数量.热区等 ...
- php bccomp的替换函数
if (!function_exists('bccomp')) { /** * 支持正数和负数的比较 * ++ -- +- * @param $numOne * @param $numTwo * @p ...
- TFS安装配置
安装TFS的准备工作: 1.需要安装IIS(必选): 2.需要安装SQL Server数据库(可选): 安装TFS的步骤如下: 选择左面的“基本”选项之后,点“启动向导”按钮,如下图: 进入”基本配置 ...
- C# Log4net根据日志等级输出到不同文件
原文地址: Log4Net.Config <?xml version="1.0" encoding="utf-8"?> <configurat ...
- SQLServer获取每组前10%的数据
sqlserver2005有关键字ntile(x)和over(partition by.. order by..)子句配合. 比如获取每个表的前10%个字段. selectid,name,colid, ...
- Flume与Logstash比较
Flume与Logstash相比,个人的体会如下: Logstash比较偏重于字段的预处理:而Flume偏重数据的传输: Logstash有几十个插件,配置灵活:FLume则是强调用户的自定义开发(s ...
- 《JAVA与模式》之简单工厂与工厂方法
一.简单工厂 1.1 使用场景 1.工厂类负责创建的对象比较少: 2.客户只知道传入工厂类的参数,对于如何创建对象(逻辑)不关心: 3.由于简单工厂很容易违反高内聚责任分配原则,因此一般只在很简单的情 ...
- WinRAR破解
新建记事本文件(txt文件),然后将文件另存为以 rarreg.key 为文件名的文件(当然由于设置的不同,可能出现你保存后的文件为 rarreg.key.txt 没关系,将其重命名,删掉.txt 会 ...