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.

Note: A leaf is a node with no children.

Example:

Input: [1,2,3]
1
/ \
2 3
Output: 25
Explanation:
The root-to-leaf path 1->2 represents the number 12.
The root-to-leaf path 1->3 represents the number 13.
Therefore, sum = 12 + 13 = 25.
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
int res = 0;
public int sumNumbers(TreeNode root) {
helper(root, 0);
return res;
} private void helper(TreeNode root, int sum) {
if (root == null) {
return;
}
int curSum = 10 * sum + root.val;
if (root.left == null && root.right == null) {
res += curSum;
}
helper(root.left, curSum);
helper(root.right, curSum);
}
}

[LC] 129. 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 (2 solutions)

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

  3. 129. Sum Root to Leaf Numbers pathsum路径求和

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

  4. LeetCode OJ 129. Sum Root to Leaf Numbers

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

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

  6. 129. Sum Root to Leaf Numbers

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

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

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

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

随机推荐

  1. 100道Java面试题整理(助力2020面试!)

    1.您对微服务有何了解? 微服务,又称微服务 架 构,是一种架构风格,它将应用程序构建为以业务领域为模型的小型自治服务集合 . 通俗地说,你必须看到蜜蜂如何通过对齐六角形蜡细胞来构建它们的蜂窝状物.他 ...

  2. PAT 2011 秋

    A : World Cup Betting #include <cstdio> #include <iostream> #include <algorithm> u ...

  3. 搭建zookeeper环境

    zookeeper是一个强一致的分布式数据库,由多个节点共同组成一个分布式集群,挂掉任意一个节点,数据库仍然可以正常工作. 独立模式 下载zookeeper打包文件,并进行解压 ➜ ~ tar -xv ...

  4. Maven高级:01.maven分模块构建&&02.私服的应用

    IntelliJ IDEA 2018.3.6 x64 07 Maven高级:01.maven分模块构建(上) 07 Maven高级:01.maven分模块构建(中) 07 Maven高级:01.mav ...

  5. java的io字符流关闭和刷新.flush();

    因为内置缓冲区的原因,如果不关闭输出流,无法写出字符到文件中. 但是关闭的流对象,是无法继续写出数据 的.如果我们既想写出数据,又想继续使用流,就需要 flush 方法了. flush :刷新缓冲区, ...

  6. ZJNU 1262 - 电灯泡——中高级

    在影子没有到达墙角前,人越远离电灯,影子越长,所以这一部分无需考虑 所以只需要考虑墙上影子和地上影子同时存在的情况 因为在某一状态存在着最值 所以如果以影子总长与人的位置绘制y-x图像 会呈一个类似y ...

  7. placeholder在IE下的兼容问题

    最近写项目要求兼容到ie8,写完了去ie测试的时候,发现了placeholder在ie下的兼容问题,为了解决,搜罗网上各种牛人的解决方案,自己总结如下: css样式(设置各浏览器下placeholde ...

  8. Linux笔记(三)——Shell编程

    预备知识 1.Shell是解释执行的脚本语言,可以直接调用Linux系统命令 2.文件以.sh结尾, #!bin/bash 标识, 说明这是一个shell脚本, 不能省略 3.执行 赋予权限,直接运行 ...

  9. UEFI boot: how does that actually work, then?

    原文地址:https://www.happyassassin.net/2014/01/25/uefi-boot-how-does-that-actually-work-then/ 翻译:https:/ ...

  10. dbgview 在windows 10 中关闭后再次打开时无法“capture kernel”

    DbgView 是一个免费的用于抓取log 的工具,可以捕获并输出OutputDebugString()函数的输出,以及输出windows driver 中dbgprint 的log,对于window ...