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. ...
随机推荐
- MacBookPro 存储空间优化
首先,打开电脑内的"终端"; 其次,逐条录入下面的命令行,执行完成后,再次查看您的储存空间,多少会有一些优化,具体会优化出多少储存空间因您日常使用而定(本人清出了5G 空间,还不错 ...
- iOS中 XMPP即时通讯实现的主要步骤
这里只是列出实现的只要步骤,不是全部代码. 首先导入XMPPFramework,及相关配置,完成后开始. 创建一个XMPPHelper 类来管理要进行的操作. XMPPHelper.h文件如下 ty ...
- 微信小程序日期选择器
/* JS代码部分 */ const date = new Date() const years = [] const months = [] const days = [] const hours ...
- 霍夫直线检测 opencv
本次实验是检测图像中的直线,用到了HoughLines()和HoughLinesP()函数,其中HoughLinesP()称为累计概率霍夫变换,实验结果显示累计概率霍夫变换要比标准霍夫变换的效果好.具 ...
- 笔记-pyrhon-lib-requests
笔记-pyrhon-lib-requests 1. 简介 Requests is the only Non-GMO HTTP library for Python, safe for hum ...
- vue 组件间数据传递
父组件向子组件传值 方法一: 子组件想要使用父组件的数据,需要通过子组件的 props 选项来获得父组件传过来的数据. 1.父组件parent.vue中代码: <template> < ...
- Servlet过滤器---简介
过滤器的基本概念 Servlet过滤器从字面上的字意理解为经过一层次的过滤处理才达到使用的要求,而其实Servlet过滤器就是服务器与客户端请求与响应的中间层组件,在实际项目开发中Servlet过滤器 ...
- 利用 ESLint 检查代码质量
原文发表于作者的个人博客:http://morning.work/page/maintainable-nodejs/getting-started-with-eslint.html 其实很早的时候就想 ...
- I2C中24C02从地址设置
从设备地址 首先,先看一下AT24C02的芯片资料,我们会发现AT24C02有三个地址A0,A1,A2.同时,我们会在资料的Device Address介绍发现I2C器件一共有七位地址码,还有一位是读 ...
- javascript实现在textarea光标位置插入文字并移动光标到文字末尾
1.背景:实现在textarea光标位置插入文字并移动光标到文字末尾 如果每次通过val("ss")赋值的形式插入文字到textarea中,会将上一次赋的值覆盖掉. 2.思路: & ...