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.

按照题目中的规则求出结果。

使用队列很容易可以得到结果。

/**
* 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;
int result = 0;
Queue queue = new LinkedList<TreeNode>();
queue.add(root); while( !queue.isEmpty() ){ int size = queue.size();
for( int i = 0;i<size;i++){
TreeNode node = (TreeNode) queue.poll();
if( node.left != null ){
node.left.val = node.val*10+node.left.val;
queue.add(node.left);
}
if( node.right != null){
node.right.val = node.right.val+node.val*10;
queue.add(node.right);
}
if( node.left == null && node.right == null ){
result+=node.val;
}
} } return result; }
}

leetcode 129. Sum Root to Leaf Numbers ----- java的更多相关文章

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

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

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

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

  5. Leetcode#129 Sum Root to Leaf Numbers

    原题地址 二叉树的遍历 代码: vector<int> path; int sumNumbers(TreeNode *root) { if (!root) ; ; path.push_ba ...

  6. LeetCode 129. Sum Root to Leaf Numbers 动态演示

    树的数值为[0, 9], 每一条从根到叶子的路径都构成一个整数,(根的数字为首位),求所有构成的所有整数的和 深度优先搜索,通过一个参数累加整数 class Solution { public: vo ...

  7. [LeetCode]129. Sum Root to Leaf Numbers路径数字求和

    DFS的标准形式 用一个String记录路径,最后判断到叶子时加到结果上. int res = 0; public int sumNumbers(TreeNode root) { if (root== ...

  8. 【LeetCode】129. Sum Root to Leaf Numbers 解题报告(Python)

    [LeetCode]129. Sum Root to Leaf Numbers 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/pr ...

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

随机推荐

  1. 有关嵌入式linux的注意点总结

    知识收集和个人学习过程遇到的问题. 仅供参考. 1.sudo apt-get update 一直无法更新 一,查看网络是否连接上 有几种网络连接方式.常用的两种有网桥网络(Bridged)和网络地址翻 ...

  2. 如何在Linux上通过grub添加内核参数

    转自Linux中国 我们可以在linux内核启动时为其提供各种各样的参数.这些参数可以自定义内核默认的行为,或者通知内核关于硬件的配置信息.内核参数应在内核启动时通过引导装载程序,如GRUB或LILO ...

  3. 提示框alertmsg

    初始化: 1.Data属性:DOM添加属性data-toggle="alertmsg",并定义type及msg参数 示例代码: <button type="butt ...

  4. uva11059

    除法(Division,uva725) 输入整数n,按从小到大的顺序输出所有形如abcde/fghij=n的表达式,其中a~j恰好为数字0~9的一个排列(可以有前导0),2<=n<=79. ...

  5. iOS数据存取和对象序列化

    一. 使用NSKeyedArchiver类操作对象的存取 特点:该类可以将对象以键值对的形式存入文件,并通过key从文件中取出,与android中的SharedPreference用法类似,而且它序列 ...

  6. hdu 2028

    PS:以前对long long型的数据就一直不怎么明白...弄了好久... long long a; scanf("%lld",&a); printf("%lld ...

  7. php大力力 [023节]CREATE TABLE创建新表sql写字段备注(2015-08-27)

    2015-08-27 php大力力023.CREATE TABLE创建新表sql写字段备注 http://www.cnblogs.com/dalitongxue/p/4762182.html 参考: ...

  8. My97DatePicker日期控件用法

    用法很简单,主要演示都在myDate.html  <meta http-equiv="content-type" content="text/html; chars ...

  9. SQL基础2

    create database fuxi --创建一个名为“fuxi”的数据库go                   --连接语句use fuxi   --使用名为“fuxi”的数据库gocreat ...

  10. java学习第六天

    目标 1.  块 2.  GC(了解) 3.  package import 4.  封装 一.块 {}  分类 1.普通块 作用: 组织代码.解决变量的作用域.节约了内存. 在同一个作用域内,不能声 ...