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) {
int sum = ;
if(root)
dfs(root, , sum);
return sum; }
void dfs(TreeNode *node, int val, int& sum) {
val *= ;
val += node->val; if (node->left == NULL && node->right == NULL) {
sum += val;
return;
} if (node->left) {
dfs(node->left, val, sum);
}
if (node->right) {
dfs(node->right, val, sum);
}
}
};

129. Sum Root to Leaf Numbers(Tree; DFS)的更多相关文章

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

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

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

  6. [LeetCode] 129. Sum Root to Leaf Numbers_Medium tag: DFS

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

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

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

  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. CentOS 中安装NFS

    NFS(network file system)网络文件系统,类似Windows中的文件夹共享,如下有三台机器A, B, C,它们需要访问同一个目录,目录中都是图片,传统的做法是把这些图片分别放到A, ...

  2. php-fpm设置与 phpMyadmin超时 操作SQL超时

    LNMP 一键安装包环境: Phpmyadmin   登录超时 (1440 秒未活动),请重新登录. vim /usr/local/php/etc/php.ini session.gc_maxlife ...

  3. RK3288 USB UVC camera 摄像头 VIDIOC_DQBUF Failed!!! err[I/O error]

    RK3288     Android5.1   多个品牌USB摄像头 同一块主板和代码,大部分品牌的USB摄像头可以正常使用,只有某一款USB摄像头不能使用. 插上摄像头,底层可以识别到摄像头. &l ...

  4. 免费数据集下载网站【dataset】

    https://github.com/awesomedata/awesome-public-datasets

  5. 【Leetcode 136】Single Number

    问题描述:给出一个整数数组,除了一个元素外,其他每个元素都出现了2次,找出只出现1次的元素. int singleNumber(vector<int>& nums); 分析:比较自 ...

  6. 保证service不被杀死的方法

    Service设置成START_STICKY kill 后会被重启(等待5秒左右),重传Intent,保持与重启前一样 提升service优先级 在AndroidManifest.xml文件中对于in ...

  7. CentOS7 php7 安装 curl 扩展

    直接从php源码包中,使用root权限安装. 找到原先安装PHP的源码包文件(如果已经删掉需要重新下载原来版本的源码包并解压) 我的php源码包在root家目录下. cd /php-7.1.4/ext ...

  8. py基础2--列表,元祖,字典,集合,文件

    本节内容 列表.元祖操作 字符串操作 字典操作 集合操作 文件操作 字符编码与转码 三元运算&生成式&成员运算&解压法&队列堆栈&数据类型转换 1. 列表操作 ...

  9. 转 - 使用from __future__ import unicode_literals时要注意的问题

    原文链接: http://www.cnblogs.com/ajianbeyourself/p/4471035.html 使用from __future__ import unicode_literal ...

  10. verilog 之数字电路 寄存器,触发器。

    我一直听说没有由code到circuit就只是入门了.实在没办法了.我想了一招,一个一个的写,然后看RTL,然后分析.这是第一篇. 1.触发器. 没有复位,置位.posedge clk 是触发沿时钟. ...