【LeetCode-面试算法经典-Java实现】【129-Sum Root to Leaf Numbers(全部根到叶子结点组组成的数字相加)】
【129-Sum Root to Leaf Numbers(全部根到叶子结点组组成的数字相加)】
【LeetCode-面试算法经典-Java实现】【全部题目文件夹索引】
原题
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.
题目大意
给定一个二叉树。每一个结点的值是0-9。根到叶子组成一个数字,求全部的根到叶子组成的数字的和。
解题思路
採用回溯法。
代码实现
算法实现类
public class Solution {
private int result = 0; // 记录总的结果
private int num = 0; // 记根到叶子的数字
public int sumNumbers(TreeNode root) {
sum(root);
return result;
}
private void sum(TreeNode root) {
if (root != null) {
num = num*10 + root.val;
// 已经到了根结点了
if (root.left == null && root.right == null) {
result += num;
}
sum(root.left);
sum(root.right);
num /= 10;
}
}
}
评測结果
点击图片,鼠标不释放,拖动一段位置,释放后在新的窗体中查看完整图片。
特别说明
欢迎转载。转载请注明出处【http://blog.csdn.net/derrantcm/article/details/47678205】
【LeetCode-面试算法经典-Java实现】【129-Sum Root to Leaf Numbers(全部根到叶子结点组组成的数字相加)】的更多相关文章
- [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 求根叶数字总和
给定一个只包含 0-9 数字的二叉树,每个根到叶的路径可以代表一个数字.例如,从根到叶路径 1->2->3则代表数字 123.查找所有根到叶数字的总和.例如, 1 / \ 2 ...
- Leetcode129. Sum Root to Leaf Numbers求根到叶子节点数字之和
给定一个二叉树,它的每个结点都存放一个 0-9 的数字,每条从根到叶子节点的路径都代表一个数字. 例如,从根到叶子节点路径 1->2->3 代表数字 123. 计算从根到叶子节点生成的所有 ...
- 【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 ...
- Java for 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 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 ...
- [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 ...
随机推荐
- where和having
where可以不能使用别名作为过滤条件,而having可以使用别名作为过滤条件. 在ORACLE中,select 语句的执行顺序是: 1. from语句 2. where语句(结合条件) 3. sta ...
- 洛谷 P2949 [USACO09OPEN]工作调度Work Scheduling
P2949 [USACO09OPEN]工作调度Work Scheduling 题目描述 Farmer John has so very many jobs to do! In order to run ...
- Spring Cloud Feign 出现ClassNotFoundException: feign.Feign$Builder错误
Spring Cloud Feign 出现ClassNotFoundException: feign.Feign$Builder错误 后来发现是POM文件写错了,修改为正确的pom,就可以了: POM ...
- android全磁盘加密
android 全磁盘加密 什么是全磁盘加密? 全磁盘加密是使用一个密钥来为android设备上全部的用户数据加密的过程.一旦设备被加密,全部的用户创建的数据都将会在提交的磁盘之前自己主动加密,在读取 ...
- Gradle 编译多个project(包括多Library库project依赖)指导
Gradle Android最新自己主动化编译脚本教程(提供demo源代码) 这篇文章我简单写了基于Gradle2.1 进行的android project和android library的编译实例, ...
- 【LDA】动手实现LDA
这段时间对LDA比較感兴趣,尝试在工作中使用它.平时做想法的高速验证,都用的是"GibbsLDA++-0.2",一个c实现版本号的LDA. 这两天用c++ stl自己写了一个单机版 ...
- poj1363——Rails
Description There is a famous railway station in PopPush City. Country there is incredibly hilly. Th ...
- linux下面增加磁盘空间
1.先看看情况 [root@localhost tmp]# fdisk -l Disk /dev/sda: 3221 MB, 3221225472 bytes 255 heads, 63 sector ...
- 15-11-23:system指令
CMD命令:开始->运行->键入cmd或command(在命令行里可以看到系统版本.文件系统版本) 1. appwiz.cpl:程序和功能 2. calc:启动计算器 3. certmgr ...
- Gradle学习总结
Gradle学习系列 (1). Gradle快速入门 (2). 创建Task的多种方法 (3). 读懂Gradle语法 (4). 增量式构建 (5). 自定义Property (6). 使用java ...