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. Eclipse打开,出现Initializing Java Tooling “has encountered a problem错误,而且鼠标悬停在没有导包的类上面不会出现import信息。

    问题1:打开eclipse,出现了Initializing Java Tooling “has encountered a problem,点开详细信息,报的是空指针异常. 问题2:鼠标悬停在没有导包 ...

  2. UITableViewCell 的selectedBackgroundView

    UITableViewCell中的selectedBackgroundView就是用于当用户点击cell的时候,选择状态的view,你可以对这个view进行颜色或者其他样式等做一些定制,可以达到点击之 ...

  3. JQuery获取当前屏幕的高度宽度的实现代码

    <script type="text/javascript"> $(document).ready(function() { alert($(window).heigh ...

  4. LeetCode——623.在二叉树中增加一行

    给定一个二叉树,根节点为第1层,深度为 1.在其第 d 层追加一行值为 v 的节点. 添加规则:给定一个深度值 d (正整数),针对深度为 d-1 层的每一非空节点 N,为 N 创建两个值为 v 的左 ...

  5. 吴裕雄--天生自然 PHP开发学习:魔术常量

    <?php echo '这是第 " ' . __LINE__ . ' " 行'; ?> <?php echo '该文件位于 " ' . __FILE__ ...

  6. Python中的encode和decode

    原文地址:http://www.cnblogs.com/tingyugetc/p/5727383.html 1.Python3中对文本和二进制数据进行了比较清晰的区分,文本总是 unicode ,由  ...

  7. The mplot3d Toolkit

    简介 正如,pyplot模块被用来绘制二维图,matplotlib使用mplot3d模块绘制三维图形,在mplot3d模块中存在 mpl_toolkits.mplot3d.axes3dmpl_tool ...

  8. echars 柱状图点击事件

     drawlineCRK() {       let _this = this;       ///绘制echarts 柱状图       let mycharts = this.$echarts.i ...

  9. 如何优雅的设计APP页面?

    页面框架设计只是整个产品设计中的一环,不要把眼界局限在这一环,也不要只站需求.只站在交互.只站在视觉上思考问题,从多个角度看问题,你才会学会成长. 产品设计是一个系统工程,单独拧出来其中一个流程来讲, ...

  10. Python笔记_第四篇_高阶编程_GUI编程之Tkinter_1.使用Python进行GUI编程的概述

    1. GUI概述: GUI全称为Graphical User Interface,叫做图形用户界面,也是一种交互方式(Interaction).早期计算机使用的命令行界面(command-line i ...