题目

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 binary tree
* 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;
}
return sum(root,0);
} public int sum(TreeNode node,int parentVal){
int sum=0;
int temp=parentVal*10+node.val;
if(node.left==null&&node.right==null){
sum=temp;
}
if(node.left!=null){
sum+=sum(node.left,temp);
}
if(node.right!=null){
sum+=sum(node.right,temp);
}
return sum;
}
}

---EOF---

【LeetCode】Sum Root to Leaf Numbers的更多相关文章

  1. 【leetcode】Sum Root to Leaf Numbers(hard)

    Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a number ...

  2. 【Leetcode】【Medium】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. 【树】Sum Root to Leaf Numbers

    题目: Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a nu ...

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

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

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

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

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

随机推荐

  1. jquery结合Highcharts插件实现动态数据仪表盘图形化显示效果

    仪表盘显示效果如图: 方法一效果图: 方法二效果图(插件版本4.0.1): ​ js代码如下: $(function(){ //方法一: var chart = new Highcharts.Char ...

  2. java泛型问题 关于警告:XX is a raw type

    (本文例子适用于JDK 5.0, 学习请先安装并配置!!!)         我们从一个简单的例子开始:假设我们现在需要一个专用来存储字符串的List,该如何实现?呵呵,这还不简单,且看如下代码:   ...

  3. Delphi 实现Ini文件参数与TEdit和TCheckBox绑定(TSimpleParam)

    在实际工作中,常遇到对Ini参数的修改,显示与保存问题. 如果手工写代码,会有很大的工作量. 本例把ini文件的参数与控件绑定起来,以达到方便使用的目的. 本例程共有2个单元 uSimpleParam ...

  4. java.util.Timer分析源码了解原理

    Timer中最主要由三个部分组成: 任务 TimerTask .  任务队列: TaskQueue queue 和 任务调试者:TimerThread thread 他们之间的关系可以通过下面图示: ...

  5. Java HashSet和LinkedHashSet的用法

    Java HashSet和LinkedHashSet的用法 类HashSet和LinkedHashSet都是接口Set的实现,两者都不能保存重复的数据.主要区别是HashSet不保证集合中元素的顺序, ...

  6. python教程,文章list

    http://www.2cto.com/kf/web/Python/ http://www.v2ex.com/go/python http://www.sharejs.com/codes/python ...

  7. CC++初学者编程教程(2) Microsoft Visual C++ 6.0开发环境搭建

    上一篇演示的是如何安装VS2010,本文演示的是如何安装Microsoft Visual C++ 6.0 简称VC6. 有同学经常VC6都是很古董的版本了,为啥他还存在,不得不说VC6是微软一个很经典 ...

  8. WISPr1.0

    王桢珍 王兵 侯志强 苑红 中国移动研究院 网络技术研究所, 北京100053 摘要   本文详细介绍了WLAN国际漫游的WISPr1.0技术规范并探讨其具体实现,包括基于WISPr1.0的WLAN国 ...

  9. ubuntu中如何关闭防火墙?

    只需要输入 root@stgman-desktop:~#  sudo ufw disable 防火墙在系统启动时自动禁用

  10. collection系列用法-deque双向队列

    deque双向队列 Deque可以从两端添加和删除元素.常用的结构,是它的简化版本. Deque支持序列的常用操作,现在举一个简单例子,你会发现其实跟平成的list没啥区别: import colle ...