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. Temporal Ensembling for Semi-Supervised Learning

    Laine, Samuli, and Timo Aila. "Temporal Ensembling for Semi-Supervised Learning." arXiv pr ...

  2. Batch Normailzation

    转自:http://blog.csdn.net/malefactor/article/details/51476961

  3. 解决使用VS2013创建的MVC项目在VS2015中打开的各种问题

    其实很多问题一直都存在,但是因为其不影响编译结果和运行结果,所以我也就一直没理它.但是问题放在那一直存在,偶尔想起来还有某些问题没解决的时候心里总是感觉不得劲,所以今天就彻底的查查资料解决了. 问题一 ...

  4. 【微信小游戏】文件系统,远程加载资源打破4M限制

    一.前提 微信小游戏,对游戏包体的大小有严格是限制,上传文件大小<4M,但是本地缓存文件有50M空间,也就是说我们可以将一些资源放到网上,然后缓存到本地. 二.官方概念 文件系统 文件系统是小程 ...

  5. ryu的RESTAPI简介——我主要用于下发和查看流表

    一.Rest API简介 REST即表述性状态传递(RepreSentational State Transfer),是一种针对网络应用的设计和开发方式,可以降低开发的复杂性,提高系统的可伸缩性. 表 ...

  6. boost::bind 详解

    使用 boost::bind是标准库函数std::bind1st和std::bind2nd的一种泛化形式.其可以支持函数对象.函数.函数指针.成员函数指针,并且绑定任意参数到某个指定值上或者将输入参数 ...

  7. 【bzoj4518】[Sdoi2016]征途 斜率优化dp

    原文地址:http://www.cnblogs.com/GXZlegend/p/6812435.html 题目描述 Pine开始了从S地到T地的征途. 从S地到T地的路可以划分成n段,相邻两段路的分界 ...

  8. wordpress添加关键字

    wordpress自动添加标签为关键字: <?php //判断是否为首页 if ( is_home ()) { $description = "jcomey一个文艺青年的个人博客&qu ...

  9. Vue.js_数据绑定

    一.文本 data {{data}} <div id="div1">{{message}}</div> <script> var div1 = ...

  10. WebBench----简洁优美的网站压力测试工具

    [root@c webbench]# webbench -c 10 -t 20 http://www.baidu.com/Webbench - Simple Web Benchmark 1.5Copy ...