LeetCode -- Sum Root to Leaf NNumbers
Related Links:
Path Sum: http://www.cnblogs.com/little-YTMM/p/4529982.html
Path Sum II: http://www.cnblogs.com/little-YTMM/p/4531115.html
Question:
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
.
Analysis:
给出一棵二叉树,其节点的值仅在0-9之间,二叉树的每条根节点到叶节点的路径都会产生一个数。
例如一条根-叶的路径1->2->3会产生数字123.
找出所有路径产生数字的和。
思路;由于以前做过path sum的题目,因此用类似的思路,用两个链表分别保存当前节点和当前已经产生的路径值,然后到叶节点时将该路径的值加到最终结果上即可。
Answer:
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public int sumNumbers(TreeNode root) {
if(root == null)
return 0;
LinkedList<TreeNode> node = new LinkedList<TreeNode>();
LinkedList<Integer> product = new LinkedList<Integer>();
node.add(root);
product.add(root.val);
int result = 0;
while(!node.isEmpty()) {
TreeNode tempNode = node.poll();
int tempProduct = product.poll();
if(tempNode.left == null && tempNode.right == null)
result += tempProduct;
if(tempNode.left != null) {
node.add(tempNode.left);
product.add(tempProduct * 10 + tempNode.left.val);
}
if(tempNode.right != null) {
node.add(tempNode.right);
product.add(tempProduct * 10 + tempNode.right.val);
}
}
return result;
}
}
LeetCode -- Sum Root to Leaf NNumbers的更多相关文章
- 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 ...
- [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
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)
题意: 给一棵二叉树,每个节点上有一个数字,范围是0-9,将从根到叶子的所有数字作为一个串,求所有串的和. 思路: 普通常规的DFS. /** * Definition for a binary tr ...
- leetcode—sum root to leaf number
题目如下: Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a ...
- 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(所有路径之和)
转载请注明来自souldak,微博:@evagle 观察题目给的返回值类型是int,可以断定这棵树的高度不会超过10,所以数据量其实是非常小的.那就直接dfs遍历这棵树,然后到叶子节点的时候将值加到最 ...
- [Leetcode] Sum root to leaf numbers求根到叶节点的数字之和
Given a binary tree containing digits from0-9only, each root-to-leaf path could represent a number. ...
随机推荐
- python解析ini文件
python解析ini文件 使用configparser - Configuration file parser sections() add_section(section) has_section ...
- jquery操作元素的位置
.offset() 在匹配的元素中,获取第一个元素的当前坐标,或设置每一个元素的坐标,坐标相对于文档. .offset() 这个不接受任何参数. var offset = p.offset(); // ...
- BZOJ1004: [HNOI2008]Cards(Burnside引理 背包dp)
Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 4255 Solved: 2582[Submit][Status][Discuss] Descript ...
- BZOJ4128: Matrix(BSGS 矩阵乘法)
Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 813 Solved: 442[Submit][Status][Discuss] Descriptio ...
- Java的“Goto”与标签
goto在Java中是一个保留字,但在语言中并没有用到它:Java没有goto.但是,Java也能完成一些类似于跳转的操作,主要是依靠:标签. 为什么要使用标签 在迭代语句中,我们可以使用break和 ...
- 基于Ceph分布式集群实现docker跨主机共享数据卷
上篇文章介绍了如何使用docker部署Ceph分布式存储集群,本篇在此基础之上,介绍如何基于Ceph分布式存储集群实现docker跨主机共享数据卷. 1.环境准备 在原来的环境基础之上,新增一台cen ...
- python__标准库 : 正则表达式(re)
re.match 尝试从字符串的起始位置匹配一个模式,如果不是起始位置匹配成功的话,match()就返回none. re.search 扫描整个字符串并返回第一个成功的匹配. 替换: re.sub(p ...
- 3.4.2 Undefined类型【JavaScript高级程序设计第三版】
Undefined 类型只有一个值,即特殊的 undefined.在使用 var 声明变量但未对其加以初始化时,这个变量的值就是 undefined,例如: var message; alert(me ...
- php正则 与 js正则
PHP中的正则表达式函数 在PHP中有两套正则表达式函数库.一套是由PCRE(Perl Compatible Regular Expression)库提供的.PCRE库使用和Perl相同的语法规则实现 ...
- POJ:3320-Jessica's Reading Problem(尺取法)
Jessica's Reading Problem Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 15224 Accepted: ...