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.

解题思路:

递归,JAVA实现如下:

    static public int sumNumbers(TreeNode root) {
if(root==null)
return 0;
return sumNumbers(root,0);
}
public static int sumNumbers(TreeNode root,int res) {
if(root.left==null&&root.right==null)
return res*10+root.val;
if(root.left==null)
return sumNumbers(root.right,res*10+root.val);
if(root.right==null)
return sumNumbers(root.left,res*10+root.val);
return sumNumbers(root.left,res*10+root.val)+sumNumbers(root.right,res*10+root.val);
}

Java for LeetCode 129 Sum Root to Leaf Numbers的更多相关文章

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

  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@ [129] Sum Root to Leaf Numbers (DFS)

    https://leetcode.com/problems/sum-root-to-leaf-numbers/ Given a binary tree containing digits from 0 ...

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

  5. Leetcode#129 Sum Root to Leaf Numbers

    原题地址 二叉树的遍历 代码: vector<int> path; int sumNumbers(TreeNode *root) { if (!root) ; ; path.push_ba ...

  6. LeetCode 129. Sum Root to Leaf Numbers 动态演示

    树的数值为[0, 9], 每一条从根到叶子的路径都构成一个整数,(根的数字为首位),求所有构成的所有整数的和 深度优先搜索,通过一个参数累加整数 class Solution { public: vo ...

  7. [LeetCode]129. Sum Root to Leaf Numbers路径数字求和

    DFS的标准形式 用一个String记录路径,最后判断到叶子时加到结果上. int res = 0; public int sumNumbers(TreeNode root) { if (root== ...

  8. 【LeetCode】129. Sum Root to Leaf Numbers 解题报告(Python)

    [LeetCode]129. Sum Root to Leaf Numbers 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/pr ...

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

随机推荐

  1. 双网卡环境导致Oracle连接异常

    现在就是流行向最高水平看齐,这次项目的部署,好好的SQL Server扔了(有正版授权的企业版,神啊...),逢人就夸:“俺们那上的可是最顶级的Oracle Database System!”.看了看 ...

  2. Android开发常见的Activity中内存泄漏及解决办法

    上一篇文章楼主提到由Context引发的内存泄漏,在这一篇文章里,我们来谈谈Android开发中常见的Activity内存泄漏及解决办法.本文将会以“为什么”“怎么解决”的方式来介绍这几种内存泄漏. ...

  3. Oracle递归操作

    需求:找出代理商中没有挂商家的代理商 简单SQL如下: select * from t_proxy tp where tp.id not in (SELECT tp.id as p_id FROM t ...

  4. New Ubuntu 16.04 Server Checklist

    新的云服务器需要进行一些配置和安装一些软件,推荐Digitalocean社区的一些教程 服务器配置(包括SSH,安全设置):https://www.digitalocean.com/community ...

  5. C 语言经典100例

    C 语言经典100例 C 语言练习实例1 C 语言练习实例2 C 语言练习实例3 C 语言练习实例4 C 语言练习实例5 C 语言练习实例6 C 语言练习实例7 C 语言练习实例8 C 语言练习实例9 ...

  6. 使用聚合数据API查询快递数据-短信验证码-企业核名

    有位朋友让我给他新开的网站帮忙做几个小功能,如下: 输入快递公司.快递单号,查询出这个快件的所有动态(从哪里出发,到了哪里) 在注册.登录等场景下的手机验证码(要求有一定的防刷策略) 通过输入公司名的 ...

  7. chm文件打不开的解决办法

    我今天在网上找了找C++函数库,下载下来一个 .chm 文件,打开之后发现只显示了目录,内容却显示不出来. 显示是这样:右边区域显示不出来. 在网上查了一下发现CHM文件是网上比较多的电子书籍显示格式 ...

  8. 【Union Find】JAVA implementation

    import java.util.ArrayList; import java.util.Arrays; import java.util.HashMap; import java.util.Scan ...

  9. ARM和STM32的区别及ARM公司架构的发展

    ARM和STM32的区别及ARM公司架构的发展 转:https://www.cnblogs.com/kwdeblog/p/5260348.html ARM是英国的芯片设计公司,其最成功的莫过于32位嵌 ...

  10. Netty(三):线程模型

    Netty中支持单线程模型,多线程模型,主从多线程模型. 1 单线程模型 在ServerBootstrap调用方法group的时候,传递的参数是同一个线程组,且在构造线程组的时候,构造参数为1,这种开 ...