问题

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

  An example is the root-to-leaf path1->2->3which represents the number123.

  Find the total sum of all root-to-leaf numbers.

  For example,

      1
   / \
   2 3

  The root-to-leaf path1->2represents the number12.
  The root-to-leaf path1->3represents the number13.

  Return the sum = 12 + 13 =25.

代码

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

树——sum-root-to-leaf-numbers(根到叶节点数字之和)的更多相关文章

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

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

  3. LeetCode OJ:Sum Root to Leaf Numbers(根到叶节点数字之和)

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

  4. C语言递归之求根到叶节点数字之和

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

  5. Leetcode之深度优先搜索(DFS)专题-129. 求根到叶子节点数字之和(Sum Root to Leaf Numbers)

    Leetcode之深度优先搜索(DFS)专题-129. 求根到叶子节点数字之和(Sum Root to Leaf Numbers) 深度优先搜索的解题详细介绍,点击 给定一个二叉树,它的每个结点都存放 ...

  6. LeetCode解题报告—— Sum Root to Leaf Numbers & Surrounded Regions & Single Number II

    1. Sum Root to Leaf Numbers Given a binary tree containing digits from 0-9 only, each root-to-leaf p ...

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

  8. 23. Sum Root to Leaf Numbers

    Sum Root to Leaf Numbers Given a binary tree containing digits from 0-9 only, each root-to-leaf path ...

  9. LeetCode: Sum Root to Leaf Numbers 解题报告

    Sum Root to Leaf Numbers Given a binary tree containing digits from 0-9 only, each root-to-leaf path ...

随机推荐

  1. WWDC2017-whats_new_in_safari_view_controller

    最后更新: 2017-08-08 官方地址: https://developer.apple.com/videos/play/wwdc2017/225/ WWDC2017中,对SafariViewCo ...

  2. 深入理解JVM虚拟机11:Java内存异常原理与实践

    本文转自互联网,侵删 本系列文章将整理到我在GitHub上的<Java面试指南>仓库,更多精彩内容请到我的仓库里查看 https://github.com/h2pl/Java-Tutori ...

  3. Azure Monitor Kibana configuration always seems to send over SSL

    https://github.com/elastic/logstash/issues/10125 https://blogs.cisco.com/security/step-by-step-setup ...

  4. Android4.0 Camera架构初始化流程【转】

    本文转载自:http://blog.chinaunix.net/uid-2630593-id-3307176.html Android Camera 采用C/S架构,client 与server两个独 ...

  5. python 浮点运算

    print(format(float(a)/float(b),'.2f'))

  6. 在阿里云centOS7上部署Redis 5.0.5主从 + 哨兵模式

    一.在两台服务器上分别安装.配置Redis 5.0.5 ,为一主一从 安装Redis关键命令: 将安装包上传至:/home 目录下解 压:.tar.gz 安装依赖:yum install gcc 安装 ...

  7. Flask框架【七】—session组件详解

    一.flask session简介 flask中session组件可分为内置的session组件还有第三方flask-session组件,内置的session组件缺点: 功能单一 session是保存 ...

  8. DRF中的视图集的使用

    1.说明:DRF框架中的视图集: 在drf开发接口中,使用GenericAPIView和视图扩展类结合起来完成接口功能是一件很常见的事情,所以,drf的作者帮我们提前把  GenericAPIView ...

  9. git总览

    git客户端官网:https://git-scm.com/ 下载对应版本安装 服务器安装git 安装依赖:yum install -y curl-devel expat-devel gettext-d ...

  10. 函数介绍——MulDiv

    http://blog.sina.com.cn/s/blog_579ebc11010008ql.html 函数介绍——MulDiv (2007-03-27 10:05:30) 转载▼   分类: 编程 ...