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 ...
随机推荐
- MySql 性能优化杂记
前一段时间接触MySql 服务器,关于查询忧化方面整理,优化主要唯绕几个工具函数 : show profiling , explain , 索引 , limit 如果上司抱怨服务器查询太慢,这时候 ...
- 阻止Infinitescroll.js无限滚动加载页面解决方法
由于某些原因,想终止当前页继续翻页的操作,可在Infinitescroll回调函数中将翻页事件取消. 代码如下: // -- 每页滚屏加载的页数-- var IpageItems = 5; var i ...
- 各种注释--------html,jsp
1. <!--到梦之都XHTML教程的链接--> <a href="http://www.dreamdu.com/xhtml/"> 学习XHTML < ...
- goldengate 12c对teradata的支持
OGG12c在2014.4.24发布了对TD的最新支持版本,软件可在此下载:https://edelivery.oracle.com.新版本特性如下: 当前支持TD 14.1的捕获和交付,仍然基于Te ...
- Integer 和int
获取Integer对象有两种方式:Integer x = 100:或者Integer x = new Integer(100): Integer x = 100:等价于Integer x = Inte ...
- T-sql语句
在用代码编辑数据库时,首先要启动WAMPSERVER 1.创建表 create table Car ( Code varchar(50) primary key , Name varchar(50) ...
- DirectX 总结和DirectX 9.0 学习笔记
转自:http://www.cnblogs.com/graphics/archive/2009/11/25/1583682.html DirectX 总结 DDS DirectXDraw Surfac ...
- Gradle: The New Android Build System
Gradle: The New Android Build System Google selected Gradle as the foundation of the Android SDK bui ...
- hdu 2049
Ps:WA了无限次...简直做到崩溃..高中学的知识都忘了....这道题就是跟2048差不多.. 从N个人里选M个人,有Cmn种选法,然后就是M的错排*Cnm 代码: #include "s ...
- KeySweeper 微软无线键盘嗅探装置
Author:Samy Kamkar From: http://samy.pl/keysweeper/ 中文字幕由Galaxy无名提供 PS:视频传到youku之后发现50秒左右的字幕被干掉了,不知道 ...