问题

  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. Http请求状态大全

    一.HTTP状态码分类 HTTP状态码由三个十进制数字组成,第一个十进制数字定义了状态码的类型,后两个数字没有分类的作用.HTTP状态码共分为5种类型: 分类 分类描述 1** 信息 服务器收到请求, ...

  2. 大数据笔记(三)——Hadoop2.0的安装与配置

    一.Hadoop安装部署的预备条件 准备:1.安装Linux和JDK. 安装JDK 解压:tar -zxvf jdk-8u144-linux-x64.tar.gz -C ~/training/ 设置环 ...

  3. SpringMVC传参注解@RequestParam,@RequestBody,@ResponseBody,@ModelAttribute

    参考文档:https://blog.csdn.net/walkerjong/article/details/7946109 https://www.cnblogs.com/daimajun/p/715 ...

  4. centos 6.8 安装 WebVirtMgr

    1.kvm虚拟机已经用 virsh命令装好了 2. WebVirtMgr的安装步骤完全参照此处: https://github.com/retspen/webvirtmgr/wiki/Install- ...

  5. java执行系统命令, 返回执行结果

    package com.geostar.gfstack.opinion.util; import java.io.BufferedReader; import java.io.Closeable; i ...

  6. element-ui的rules全局验证

    原文:https://www.jianshu.com/p/6a29e9e51b61 rules.js var QQV = (rule, value, callback) => { debugge ...

  7. The MEAN stack is a modern replacement for the LAMP (Linux, Apache, MySQL, PHP/Python) stack

    w https://www.mongodb.com/blog/post/building-your-first-application-mongodb-creating-rest-api-using- ...

  8. git 还原、恢复、回退

    通过git revert来实现线主干代码的回滚.如下命令 对于 merge类型的commit对象,还需要"-m"参数 git revert -m 1  commit-id 对于普通 ...

  9. jQuery测试错题解析

    1. JavaScript中实现回车切换效果是利用了event对象的( )属性. A.Tab B.keyCode C.KeyCode D.KeyDown 解析:实现回车切换效果是keyCode属性.故 ...

  10. charles_01_打断点修改接口请求&返回数据

    前言 测试过程中,为了模拟某场景测试,经常需要修改接口请求或者返回数据.可通过抓包工具打断点,实现模拟测试场景.常用的抓包工具charles和fiddler均可打断点mock数据.由于小编安装了cha ...