给定一个只包含 0-9 数字的二叉树,每个根到叶的路径可以代表一个数字。
例如,从根到叶路径 1->2->3则代表数字 123。
查找所有根到叶数字的总和。
例如,
    1
   / \
  2   3
根到叶子路径 1->2 表示数字 12。
根到叶子路径 1->3 表示数字 13。
返回总和 = 12 + 13 = 25。
详见:https://leetcode.com/problems/sum-root-to-leaf-numbers/description/

Java实现:

/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public int sumNumbers(TreeNode root) {
if(root==null){
return 0;
}
return helper(root,0);
}
private int helper(TreeNode root,int sum){
if(root==null){
return 0;
}
if(root.left==null&&root.right==null){
return sum*10+root.val;
}
return helper(root.left,sum*10+root.val)+helper(root.right,sum*10+root.val);
}
}

129 Sum Root to Leaf Numbers 求根叶数字总和的更多相关文章

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

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

  3. [Leetcode] Sum root to leaf numbers求根到叶节点的数字之和

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

  4. Leetcode129. Sum Root to Leaf Numbers求根到叶子节点数字之和

    给定一个二叉树,它的每个结点都存放一个 0-9 的数字,每条从根到叶子节点的路径都代表一个数字. 例如,从根到叶子节点路径 1->2->3 代表数字 123. 计算从根到叶子节点生成的所有 ...

  5. 【LeetCode】129. Sum Root to Leaf Numbers 解题报告(Python)

    [LeetCode]129. Sum Root to Leaf Numbers 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/pr ...

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

  7. 129. Sum Root to Leaf Numbers(从根节点加到叶子节点的和)

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

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

随机推荐

  1. android自己定义开关控件

    近日在android项目要使用开关控件.可是android中自带的开关控件不太惬意,所以就打算通过自己定义View写一个开关控件 ios的开关控件当然就是我要仿照的目标. 先上图:   waterma ...

  2. CentOS笔记-目录结构(转载了菜鸟教程里的)

    在linux系统中,有几个目录是比较重要的,平时需要注意不要误删除或者随意更改内部文件. /etc: 上边也提到了,这个是系统中的配置文件,如果你更改了该目录下的某个文件可能会导致系统不能启动. /b ...

  3. 关于Cascading

    Cascading是一个开源的Java库和应用程序编程接口(API),它为MapReduce提供了一个抽象层.它允许开发者构建出能在Hadoop集群上运行的复杂的.关键任务的数据处理应用. Casca ...

  4. VS 预先生成事件命令

    宏 说明 $(ConfigurationName) 当前项目配置的名称(例如,“Debug|Any CPU”). $(OutDir) 输出文件目录的路径,相对于项目目录.这解析为“输出目录”属性的值. ...

  5. DDD领域建模基本流程

    整理一个精简的DDD领域建模基本流程,供大家在DDD领域建模实践中进行参考. 搜集用户故事(用户的原始需求) 整理用户故事,抽出用例(用例表达了用户对系统的需求,定义了系统的边界以及系统外部角色和系统 ...

  6. java java.io.IOException: No locks available异常处理解决

    try {    randomAccessFile = new RandomAccessFile(file, "rw");    lock = randomAccessFile.g ...

  7. c语言基本函数

    1. 用宏定义写出swap(x,y) #define swap(x, y) x = x + y; y = x - y; x = x - y; 2.数组a[N],存放了1至N-1个数,其中某个数重复一次 ...

  8. eclipse新建安卓项目helloworld

    第一步:安装JDK. 第二步:配置Windows上JDK的变量环境 . 第三步: 下载安装Eclipse . 第四步:下载安装Android SDK (下载Android SDK Manager工具, ...

  9. 百度地图API--信息窗口

    信息窗口 -----纯文本信息窗口 为了更方便的提示用户,在地图的指定的地方添加文本信息的窗口,给用户直观展示信息,下面是如何添加一个纯文本的信息窗口. 下面是添加一个文本窗口: /* * 添加纯文本 ...

  10. [Selenium] common functions comparison

    1.Wait for element  in default time or self defined time When the element need some time to be prese ...