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. Java日志框架介绍

    一.序言 日志为系统的必不可少的一部分,通过输出的日志我们可以排查线上出现的各种问题,就像断案的线索一样.我们还可以通过日志数据分析用户的行为习惯做大数据分析. 二.日志框架分类及其历史 框架的种类: ...

  2. Oracle口令文件管理

    Oracle的口令文件目录 $ORACLE_HOME/dbs/orapw$ORACLE_SID 建立口令文件 orapwd file=$ORACLE_HOME/dba/orapw$ORACLE_SID ...

  3. 银行卡验证API

    一.银联开放平台 https://open.unionpay.com/tjweb/api/detail?apiSvcId=21 应用场景 综合数据服务平台是银联为接入商户提供的综合数据认证服务接口,目 ...

  4. lvs+ipvsadm负载均衡

    使用LVS实现负载均衡原理及安装配置详解 负载均衡集群是 load balance 集群的简写,翻译成中文就是负载均衡集群.常用的负载均衡开源软件有nginx.lvs.haproxy,商业的硬件负载均 ...

  5. ajax原生js及readystate/status

    菜鸟教程 ←← GET: <script> function  ajaxGet(){ var  xmlhttp; if(window.XMLHttpRequest){ //TE7+  Fi ...

  6. Lucene实战

    导包

  7. 微信小程序推广方案

    拥有小程序只是基础,能玩转小程序运营才是关键.本文将会简单讲述十种最实用的小程序推广策略,结合具体案例阐述商家企业如何在拥有小程序后玩转小程序,快速实现小程序的推广. 一. 公众号+小程序 小程序可以 ...

  8. 嵌入式Linux编译内核步骤 / 重点解决机器码问题 / 三星2451

    嵌入式系统更新内核 1. 前言 手里有一块Friendly ARM的MINI2451的板子,这周试着编译内核,然后更新一下这个板子的Linux内核,想要更新Linux Kernel 4.1版本,但是种 ...

  9. STM32遇到的问题

    1.GPIO输出实验的时候,原来的库和现成的源代码有出入?导致实验现象不同,delay_ms,主要集中在这个函数上面 2.按键输入的时候,LED和KEY 初始化全部放在主函数,有按下按键以后,灯闪烁不 ...

  10. PHP.20-图片上传下载

    图片上传下载 思路: 1.创建图片上传的存放目录 /uploads/ 2.index.php //浏览页面,提供上传表单 上传表单:文件上传必须使用enctype="multipart/fo ...