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.

 class Solution {
public int sumNumbers(TreeNode root) {
return sum(root,0);
}
private int sum(TreeNode root,int s) {
//s 上一层的累积和
if(root==null) return 0;
if(root.left==null && root.right==null) //遍历到叶子节点
return s*10+root.val;
return sum(root.left,s*10+root.val)+sum(root.right,s*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. 129 Sum Root to Leaf Numbers 求根叶数字总和

    给定一个只包含 0-9 数字的二叉树,每个根到叶的路径可以代表一个数字.例如,从根到叶路径 1->2->3则代表数字 123.查找所有根到叶数字的总和.例如,    1   / \  2  ...

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

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

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

  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] Sum Root to Leaf Numbers 求根到叶节点数字之和

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

  7. LeetCode OJ 129. Sum Root to Leaf Numbers

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

  8. 129. Sum Root to Leaf Numbers pathsum路径求和

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

  9. [LC] 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. pl/sql 实例解析 01

    1. 合并 firstname, lastname. 1: declare 2: v_first_name varchar2(255); 3: v_last_name varchar2(255); 4 ...

  2. SQLServer2008设置开启远程连接

    SQL Server 2008默认是不允许远程连接的,sa帐户默认禁用的,如果想要在本地用SSMS连接远程服务器上的SQL Server 2008,需要做两个部分的配置:    1,SQL Serve ...

  3. 嵌入式开发之davinci--- 8148/8168/8127 中的图像采集格式Sensor信号输出YUV、RGB、RAW DATA、JPEG 4种方式区别

    简单来说,YUV: luma (Y) + chroma (UV) 格式, 一般情况下sensor支持YUV422格式,即数据格式是按Y-U-Y-V次序输出的RGB: 传统的红绿蓝格式,比如RGB565 ...

  4. Ubuntu14.4下搭配WEB服务器(apache + php + mysql)

    今天,趁着自己动手安装web服务器的余热,将Ubuntu14.4搭配WEB服务器的过程记录下来. “一切皆文件”. 说明:网上关于类似搭配web服务器的教程,案例不计其数,但自己亲自动手“试试”,一定 ...

  5. 苹果手机输入中文不会触发onkeyup事件

    今天同事的项目有这个问题,用我的安卓手机输入中文是ok的,但是苹果手机就不行   使用keyup事件检测文本框内容:  $('#keyup_i').bind('keyup', function(){ ...

  6. poj 3308(最小点权覆盖、最小割)

    题目链接:http://poj.org/problem?id=3308 思路:裸的最小点权覆盖,建立超级源点和超级汇点,将源点与行相连,容量为这行消灭敌人的代价,将列与汇点相连,容量为这列消灭敌人的代 ...

  7. WPF之数据绑定

    WPF学习之数据绑定 1. Banding基础 WPF中的数据绑定提供了很强大的功能.与普通的WinForm程序相比,其绑定功能为我们提供了很多便利,例如Binding对象的自动通知/刷新,Conve ...

  8. Android开发:《Gradle Recipes for Android》阅读笔记(翻译)4.3——排除任务

    问题: 你想要在构建进程当中排除指定的任务. 解决方案: 可以使用-x排除单个任务.编辑task graph排除多个任务. 讨论: Gradle的构建进程包含了许多任务的执行顺序.他们中的大多数都依赖 ...

  9. 2154: Crash的数字表格

    2154: Crash的数字表格 Time Limit: 20 Sec  Memory Limit: 259 MBSubmit: 3372  Solved: 1258[Submit][Status][ ...

  10. Leetcode-Permuation Sequence

    The set [1,2,3,…,n] contains a total of n! unique permutations. By listing and labeling all of the p ...