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. ...
随机推荐
- block简介
ios4.0系统已开始支持block,在编程过程中,blocks被Obj-C看成是对象,它封装了一段代码,这段代码可以在任何时候执行.Blocks可以作为函数参数或者函数的返回值,而其本身又可以带输入 ...
- 转 Ubuntu 下 vim 搭建python 环境 配置
1. 安装完整的vim# apt-get install vim-gnome 2. 安装ctags,ctags用于支持taglist,必需!# apt-get install ctags 3. 安装t ...
- C#基础-面向对象-封装
封装 命名空间 using System; using System.Collections.Generic; using System.Text; namespace ConsoleApp6 { c ...
- JS下载文件常用的方式
下载附件(image,doc,docx, excel,zip,pdf),应该是实际工作中经常遇到一个问题:这里使用过几种方式分享出来仅供参考; 初次写可能存在问题,有问题望指出 主要了解的几个知识 ...
- HTML基本教程,及一些基本常用标签。
HTML基本结构,及常用标签 <DOCTYPE html> <html> <head> <meta charset="UTF-8" /&g ...
- php结合redis实现高并发下的抢购、秒杀功能【转】
抢购.秒杀是如今很常见的一个应用场景,主要需要解决的问题有两个:1 高并发对数据库产生的压力2 竞争状态下如何解决库存的正确减少("超卖"问题)对于第一个问题,已经很容易想到用缓存 ...
- 如何对比两个Jar包
如果对比两个jar包呢?jar 都是class 文件,我对比jar,就是想知道,它增加了删除了哪些方法.增加了哪些类,删除了哪些类. 有很多方法,你可以,反编译,然后通过beyongCompair 去 ...
- oracle 用户被锁定解锁方法
修改了用户密码,第二天过来发现用户被锁定,晚上走的时候还好好的 . alter profile DEFAULT limit FAILED_LOGIN_ATTEMPTS UNLIMITED; alter ...
- virtualenv简介以及一个比较折腾的scrapy安装方法
本文来自网易云社区 作者:沈高峰 virtualenv + pip 安装python软件包是一种非常好的选择,在大部分情况下安装python软件包是不需要求助于sa的. 使用自己的一个工作副本也是写p ...
- Pascal小游戏 双人射击
一个双人的游戏 Pascal源码附上 只要俩人不脑残,一下午玩不完...又是控制台游戏中的一朵奇葩. Free Pascal 射击游戏 Program shooting_game; uses crt; ...