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的更多相关文章

  1. 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 ...

  2. [leetcode]Sum Root to Leaf Numbers @ Python

    原题地址:http://oj.leetcode.com/problems/sum-root-to-leaf-numbers/ 题意: Given a binary tree containing di ...

  3. [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 ...

  4. 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 ...

  5. LeetCode Sum Root to Leaf Numbers(DFS)

    题意: 给一棵二叉树,每个节点上有一个数字,范围是0-9,将从根到叶子的所有数字作为一个串,求所有串的和. 思路: 普通常规的DFS. /** * Definition for a binary tr ...

  6. leetcode—sum root to leaf number

    题目如下: Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a ...

  7. 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 ...

  8. leetcode Sum Root to Leaf Numbers(所有路径之和)

    转载请注明来自souldak,微博:@evagle 观察题目给的返回值类型是int,可以断定这棵树的高度不会超过10,所以数据量其实是非常小的.那就直接dfs遍历这棵树,然后到叶子节点的时候将值加到最 ...

  9. [Leetcode] Sum root to leaf numbers求根到叶节点的数字之和

    Given a binary tree containing digits from0-9only, each root-to-leaf path could represent a number. ...

随机推荐

  1. SqlServer中怎么删除重复的记录(表中没有id)

    SqlServer中怎么删除重复的记录(表中没有id) 其实我在别的网址也查到过删除重复的记录,不知道我是我SqlServer2012版本太低还是啥原因 delete from scwhere (c# ...

  2. motto - Express 4.x Request对象获得参数方法

    本文搜索关键字:motto express node js nodejs javascript request body request.body 1. req.param() 该方法获得参数最为方便 ...

  3. HTML+CSS : 笔记整理(2 常规流,BFC,固定定位,z-index)

    BFC和常规流的关系是什么:常规流遵循BFC,IFC规则. 定位规则总体来说三种: 常规流,浮动,绝对定位(CSS3里面新加了一种flex) 其中常规流包括BFC,IFC等规则,块级元素一个一排地从上 ...

  4. HTML+CSS : 笔记整理(1)

    meta:页面描述信息(可以在里面加入作者信息等,如: <meta name="description"content="HTML examples"&g ...

  5. datetime模块及time模块

    pyhton的datetime模块分析(小女子的测试之路):https://www.cnblogs.com/cindy-cindy/p/6720196.html python时间模块小结(time a ...

  6. Codeforces Round #460 (Div. 2).E 费马小定理+中国剩余定理

    E. Congruence Equation time limit per test 3 seconds memory limit per test 256 megabytes input stand ...

  7. B1076 Wifi密码 (15分)

    B1076 Wifi密码 (15分) 下面是微博上流传的一张照片:"各位亲爱的同学们,鉴于大家有时需要使用 wifi,又怕耽误亲们的学习,现将 wifi 密码设置为下列数学题答案:A-1:B ...

  8. [Codeforces976E]Well played!(贪心)

    [不稳定的传送门] Solution 首先可以证明,hp翻倍的操作一定是在同一个生物上最优 Code #include <cstdio> #include <algorithm> ...

  9. 无法访问hadoop yarn8088端口的解决方法

    1.检查是否正确的启动了resourcemanager服务 若是没有启动,请检查yarn-site-xml配置 2.若是启动了 1.检查客户机和虚拟机之间是否能够相互ping通 2.检查虚拟机防火墙是 ...

  10. Java学习笔记17---成员方法的重载与重写

    重载是指,一个类中定义了一个成员方法后,通过修改参数个数.参数类型或参数顺序,重新实现该方法,则这两个方法互为对方的重载方法. 重写是指,子类重新实现父类的成员方法. 重载后的方法,与原方法相比: ( ...