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. JQ 时间插件

    <script type="text/javascript" charset="utf-8" src="__PUBLIC__/ueditor/u ...

  2. 安装loadrunner时出现”命令行选项语法错误键入命令 \?获得帮助“的解决方法

    安装LR11 时,安装Microsoft Visual c++2005 sp1运行时组件,就会提示命令行选项语法错误,键入“命令/?”可获取帮肋信息1.进入loadrunner-11\Addition ...

  3. QT creator 编辑多个UI 文件 出现 无法解析的外部符号的错误

    创建一般的Qt Gui 程序一般会默认一个UI 文件 ,但是随着应用程序窗口的增多,同时编辑多个UI 界面是必须的. 假设我们已经创建好了一个QTUI的工程,里面已经默认了一个UI文件,但是想在添几个 ...

  4. Python中3元运算符的实现

    这是今天在温习lambda表达式的时候想到的问题,众所周知C系列语言中的 三元运算符(?:)是一个非常好用的语句, 关于C中的三元运算符表达式1?表达式2:表达式3 那么在python应该如何实现呢, ...

  5. 《Windows游戏编程大师技巧》学习笔记——关于创建显示表面

    1.如你所知,显示在屏幕上的图像仅仅只是是以某种格式存储在内存中的有色像素组成的矩阵.或是调色板化的或是RGB模式的.在不论什么一种情况下.要想做点什么你都必须知道如何绘制图到内存中,然而Direct ...

  6. CentOS7 以下安装Mysql MMM

    參考文档 參考1 參考2 * 一. 规划 server IP 作用 monitor 192.168.1.210 监控server master-001 192.168.1.211 读写主机001 ma ...

  7. 【Python】求素数-未经过任何优化

    print 'Find prime number smaller then input number \n' print 'Please input a number:' import time nu ...

  8. jquery获取对象的方法足以应付常见的各种类型的对象

    简单对象获取 $("element:first") 获取页面上某个元素的第一个如$("div:frist")表示第一个div $("element:l ...

  9. Autorotation and Autosizing

    配置应用级别的旋转方向——Global Setting 方法:点击项目-General-Deployment-Device Orientation It doesn’t necessarily mea ...

  10. Python+selenium之读取配置文件内容

    Python+selenium之读取配置文件内容 Python支持很多配置文件的读写,此例子中介绍一种配置文件的读取数据,叫ini文件,python中有一个类ConfigParser支持读ini文件. ...