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.

简单的用递归就可以实现,代码如下:

 class Solution {
public:
int sumNumbers(TreeNode* root) {
dfs(root, );
} int dfs(TreeNode * root, int curr){
if(!root)
return ;
if(!root->left && !root->right)
return curr * + root->val;
return dfs(root->left, curr * + root->val) + dfs(root->right, curr * + root->val);
}
};

java版本的代码如下所示:

 public class Solution {
public int sumNumbers(TreeNode root) {
return dfs(root, );
} public int dfs(TreeNode root, int curr){
if(root == null)
return ;
if(root.left == null && root.right == null)
return curr * + root.val;
return dfs(root.left, curr * + root.val) + dfs(root.right, curr * + root.val);
}
}

LeetCode OJ:Sum Root to Leaf Numbers(根到叶节点数字之和)的更多相关文章

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

  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】Sum Root to Leaf Numbers(hard)

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

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

  7. 【LeetCode】Sum Root to Leaf Numbers

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

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

  9. C语言递归之求根到叶节点数字之和

    题目描述 给定一个二叉树,它的每个结点都存放一个 0-9 的数字,每条从根到叶子节点的路径都代表一个数字. 例如,从根到叶子节点路径 1->2->3 代表数字 123. 计算从根到叶子节点 ...

随机推荐

  1. 李治军老师操作系统课程资源分享(视频+pdf)

    最近别人推荐,看看了哈工大的李治军老师主讲的操作系统,李治军老师通过linux0.11内核源码的讲解,学习了很多,更加形象了解了理论知识. 分享给大家,有pdf 链接:https://pan.baid ...

  2. 中文价格识别为数字 java代码

    运行效果: public class VoicePriceRecognition { private final static String NOT_HAS_PRICE_CONTENT="n ...

  3. NumPy基础知识:数组和矢量计算

    NumPy 的ndarray:一种多维数组对象 该对象是一个快速且灵活的大数据容器,可以利用这种数组对整个数据进行科学计算,语法跟标量元素之间的计算一样. 创建ndarray的方法: array函数: ...

  4. assign,copy,retain的区别以及weak和strong的区别

    @property (nonatomic, assign) NSString *title;    什么是assign,copy,retain之间的区别?      assign: 简单赋值,不更改索 ...

  5. C#将图片白色背景设置为透明

    Image image = System.Drawing.Image.FromFile(@"C:\A.JPG"); Bitmap pbitmap = new Bitmap(imag ...

  6. document.documentElement和document.body区别以及获取浏览器的宽高

    原文:http://www.jb51.net/article/41410.htm 1.区别: body是DOM对象里的body子节点,即 <body> 标签: documentElemen ...

  7. 推荐一款轻量级PHP数据库框架–Medoo

    引用官网的简介: 可以加快开发速度的最轻量级的PHP数据库框架 为什么选择Medoo及其主要功能: 轻量级–单个文件,只有20KB 易用–非常容易学习和使用 功能强大–支持各种常见和复杂的SQL查询 ...

  8. iOS 9 的新功能 universal links

    什么是 universal links: (通用链接) 一种能够方便的通过传统 HTTP 链接来启动 APP, 使用相同的网址打开web page和 APP的方式. 第一点,链接打开网址 顾名思义 第 ...

  9. Windos Server 2008 配置定时清理任务

    系统环境:Windos 2008 R2 x64 位 实施方案:自动清理超过两周的备份系统文件. 编写自动清理脚本..bat文件后缀. 打开计划任务

  10. Windows10提示“没有权限使用网络资源”的解决方案

    1.点击“开始→运行”,在“运行”对话框中输入“GPEDIT.MSC”,打开组策略编辑器 2.依次选择“计算机配置→Windows设置→安全设置→本地策略→用户权利分配” 3.双击“拒绝从网络访问这台 ...