Sum Root to Leaf Numbers

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.

SOLUTION 1:

使用递归完成。我们要做的是 把左右子树的path加起来。

base case: root是叶子节点时,直接计算path。我们用pre保留上一层计算出来的数字的值。那么到了当前根节点应该就是

把上一层的值*10再加上root的值。

 /**
* 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) {
return dfs(root, 0);
} public int dfs(TreeNode root, int pre) {
if (root == null) {
return 0;
} int cur = pre * 10 + root.val;
if (root.left == null && root.right == null) {
return cur;
} return dfs(root.left, cur) + dfs(root.right, cur);
}
}

GITHUB:

https://github.com/yuzhangcmu/LeetCode_algorithm/blob/master/tree/SumNumbers_1208_2014.java

LeetCode: Sum Root to Leaf Numbers 解题报告的更多相关文章

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

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

  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] 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. [leetcode]Sum Root to Leaf Numbers @ Python

    原题地址:http://oj.leetcode.com/problems/sum-root-to-leaf-numbers/ 题意: Given a binary tree containing di ...

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

  6. LeetCode: Sum Root to Leaf Numbers [129]

    [题目] Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a n ...

  7. [Leetcode] Sum root to leaf numbers求根到叶节点的数字之和

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

  8. LeetCode :: Sum Root to Leaf Numbers [tree、dfs]

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

  9. [LeetCode] Sum Root to Leaf Numbers dfs,深度搜索

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

随机推荐

  1. linux下yum命令出现Loaded plugins: fastestmirror Determining fastest mirrors

    今天yum install的时候出问题了,找了半天才找到一个可行的解决办法 fastestmirror是yum的一个加速插件,这里是插件提示信息是插件不能用了. 不能用就先别用呗,禁用掉,先yum了再 ...

  2. Python学习笔记(四)——编码和字符串

    一.编码 1.编码类别: (1)ASCII码:127个字母被编码到计算机里,也就是大小写英文字母.数字和一些符号 (2)GB2312码:中国制定的用于加入中文汉字的编码 (3)Unicode:防止由于 ...

  3. 【java】详解JDK的安装和配置

    目录结构: contents structure [+] 什么是JDK JDK的三个版本 JDK包含的主要内容 JDK的安装 JDK的配置 配置JAVA_HOME 配置PATH 到底自己需不需要配置C ...

  4. MongoDB学习笔记(6)--find

    MongoDB 查询文档 MongoDB 查询文档使用 find() 方法. find() 方法以非结构化的方式来显示所有文档. 语法 MongoDB 查询数据的语法格式如下: db.collecti ...

  5. IntelliJ IDEA推荐插件

    JRebel for IntelliJ 一款热部署插件,只要不是修改了项目的配置文件,用它都可以实现热部署.收费的,破解比较麻烦.不过功能确实很强大.算是开发必备神器了.热部署快捷键是control+ ...

  6. appium通过同级别(兄弟关系)元素找到元素

    在做appium测试用例的时候,要获取金额值,用uiautomatorviewer查看该元素,该元素没有特别明显的个性特点,唯一有特点的定位是有content-desc值,但是该值是变动的,所以无法通 ...

  7. 设置eclipse/myeclipse的智能提示

    打开eclipse/myeclipse选择 window-->Preferences-->JAVA-->Editor-->单击Content Assist–>在右边找到A ...

  8. linux环境下matlab连接mysql

    因为matlab是基于java的,但是原生的matlab是没有jdbc的,这是一个java的mysql connection. 只有matlab有这个包,才能正确的连接mysql. 1.在http:/ ...

  9. FIR定点提高精度的trick_02

    作者:桂. 时间:2018-02-05  19:36:08 链接:http://www.cnblogs.com/xingshansi/p/8419182.html 一.概述 本文简要记录FIR的小tr ...

  10. oracle PLSQL 多结果集嵌套循环处理优化

    oracle多结果集嵌套循环处理优化 --性能差 begin for a in (select id,name,sex,idcard from people) loop for b in (selec ...