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. JavaScript Window History 浏览器的历史

    window.history 对象在编写时可不使用 window 这个前缀. 为了保护用户隐私,对 JavaScript 访问该对象的方法做出了限制. 一些方法: history.back() - 与 ...

  2. Seqlite学习

    之前没有接触过数据库编程,尼玛,面试神码的最恶心了,非得神码都懂点,好吧,最近开始研究下,先从SQLite开始吧,贴上找到SQliteDB.之后搜集资料,慢慢学习!

  3. Sentinel 简介与API订阅发布

    Sentinel 简介 Redis 的 Sentinel 系统用于管理多个 redis 服务器(instance), 该系统执行以下三个任务: 监控(Monitoring): Sentinel 会不断 ...

  4. 【NotePade++】NotePade++如何直接编译运行java文件

    安装Notepad++和JDK(略): Notepad++的菜单栏:插件->Plugin Manager->Show Plugin Manager,Available中勾选NppExec, ...

  5. Checkstyle-Configuration

    <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE module PUBLIC "-/ ...

  6. mysql升级5.5

    对付Linux的问题,其实很多都是权限问题,细心想一下即可. centos6.4默认装的是mysql5.1,使用 yum update 也update不了.google了一下,找到个yum安装的方法: ...

  7. 跟我学SharePoint 2013视频培训课程—— 审批、拒绝列表项(13)

    课程简介 第13天,怎样在SharePoint 2013中审批.拒绝列表项. 视频 SharePoint 2013 交流群 41032413

  8. FreeSWITCH网关参数之caller-id-in-from

    1. 这个配置项两个设置值: true和false(默认) <param name="caller-id-in-from" value="true"/&g ...

  9. Intellij idea 配置热部署

    1. 采用外部tomcat的配置 1)打开右上角Run的Edit Configuration进入Tomcat配置选项页面 2)将On frame   deactivation选项更改为 Update ...

  10. 转:jQuery插件之Wookmark:流布局插件遇到图片资源请求过慢导致最终计算图片绝对位置top不够准确发生图片重叠的解决方案

    谈起Wookmark我想做过前端的大侠都不会觉得陌生,它就是远近闻名的流布局jQuery插件,这个插件使用起来非常简单,需要引入两个js: 1.<script src="/js/jqu ...