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.
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的更多相关文章
- 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 求根到叶节点数字之和
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 ...
- Leetcode#129 Sum Root to Leaf Numbers
原题地址 二叉树的遍历 代码: vector<int> path; int sumNumbers(TreeNode *root) { if (!root) ; ; path.push_ba ...
- LeetCode 129. Sum Root to Leaf Numbers 动态演示
树的数值为[0, 9], 每一条从根到叶子的路径都构成一个整数,(根的数字为首位),求所有构成的所有整数的和 深度优先搜索,通过一个参数累加整数 class Solution { public: vo ...
- [LeetCode]129. Sum Root to Leaf Numbers路径数字求和
DFS的标准形式 用一个String记录路径,最后判断到叶子时加到结果上. int res = 0; public int sumNumbers(TreeNode root) { if (root== ...
- 【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 ...
随机推荐
- sqlite3 SQL常用语句
1. select SELECT LastName,FirstName FROM Persons; SELECT * FROM Persons; 2. where SELECT * FROM Pers ...
- jQuery 其他操作
1. 样式操作 1.1 获取 class 和设置 class : class 是元素的一个属性, 所以获取 class 和设置 class 都可以使用 attr() 方法来完成. 1.2 追加样式: ...
- 番茄工作法和Bullet Journal笔记法
番茄工作法是一套时间管理方法,Bullet journal是一套笔记记录的方法,最近通过学习这2个方法来管理自己的时间,让自己战胜拖延症. 番茄工作法 番茄工作法的整体思路就是将时间分割成一个个小时间 ...
- unix shell-01 file
1 一个文件有三种访问方式: 1.读,可以显示该文件的内容 2.写,删除或者编辑这个文件 3.执行,如果该文件时一个shell脚本或程序 按照文件所针对的用户,用户可以分为三种: 1.文件属主,即该文 ...
- 用C#感受MongoDB MapReduce之魅力 转
MapReduce这个名词随着hadoop的用户的增多,越来越被人关注.MapReduce可谓MongoDB之中的亮点,我也想深入了解MapReduce,加上MongoDB操作简单,所以就选择了它.M ...
- lightoj1027
//Accepted 1688 KB 0 ms //概率简单题 //假设我们在n个门前加个起点,在n个门后加个终点,起点可以到达n个门, //为正的门可以到达终点,为负的回到起点 //则假设我们从起点 ...
- Math 对象的方法
Math 对象的方法 方法 描述 abs(x) 返回数的绝对值 acos(x) 返回数的反余弦值 asin(x) 返回数的反正弦值 atan(x) 以介于 -PI/2 与 PI/2 弧度之间的数值来返 ...
- MATLAB里的正则表达式 [转]
正则表达式在处理字符串及文本时显得十分方便,在perl, python等脚本语言,以及java, .net等平台上都支援正则表达式.事实上,在MATLAB中也提供了正则表达式的支持.主要包含三个常用的 ...
- libbspatch.so
http://www.zhihu.com/question/21154099 http://blog.csdn.net/hmg25/article/details/8100896 91助手和Googl ...
- python global vs nonlocal (2)
上一篇中简述了这两个关键字的基本用法: global用于在局部作用于修改或重定义全局变量 nonlocal用于在内部作用域修改或重定义外部变量(全局变量除外) 这只是很浅的理解方式 注意python的 ...