给定一个只包含 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. Session Timeout 与 $.ajaxSetup

    对于session过期跳转的问题,很简单,就是一个过滤器,然后判断session为空?跳转:继续.但是对于ajax的请求,需要做特殊处理,见下面代码中的 // 此处考虑ajax操作session过期的 ...

  2. 20170221 SE03 打包请求

    请求处理:SE03 只会有其中两三个功能,还需要学习. 请求打包,是传输请求时候常用的一种方法. 步骤如下: 注意点:1.打包请求一定仔细检查,不要遗漏           如果请求不多,分开传是更好 ...

  3. MySQL alter语句

    1:删除列 ALTER TABLE [表名字] DROP [列名称] 2:增加列 ALTER TABLE [表名字] ADD [列名称] INT NOT NULL  COMMENT '注释说明' 3: ...

  4. redis---01

    redis是什么: redis是开源,BSD许可,高级的key-value存储系统. 可以用来存储字符串,哈希结构,链表,集合,因此,常用来提供数据结构服务. redis和memcached相比,的独 ...

  5. Java中需要了解的点

    1.32位jvm.64位区别? 2.

  6. adb client, adb server, adbd原理浅析(附带我的操作过程)【转】

    本文转载自:http://blog.csdn.net/stpeace/article/details/24933813 adb是什么? adb就是Android调试桥,很形象啊. 先来看adb原理的逻 ...

  7. js程序开发-1

    <h1>数组的常用操作</h1> push() 方法可向数组的末尾添加一个或多个元素,并返回新数组的长度. unshift() 方法可向数组的开头添加一个或更多元素,并返回新数 ...

  8. 并不对劲的bzoj5475:loj2983:p5206:[wc2019]数树

    题目大意 task0:有两棵\(n\)(n\leq10^5)个点的树\(T1,T2\),每个点的点权可以是一个在\([1,y]\)里的数,如果两个点既在\(T1\)中有直接连边,又在\(T2\)中有直 ...

  9. [Selenium] Actions.doubleClick

    WebElement el = page.getTable_AssetMixesName().get(index); Actions action = new Actions(driver); act ...

  10. BZOJ_3063_[Usaco2013]Route Designing_DP

    BZOJ_3063_[Usaco2013]Route Designing_DP Description After escaping from the farm, Bessie has decided ...