Question

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

An example is the root-to-leaf path1->2->3which represents the number123.

Find the total sum of all root-to-leaf numbers.

For example,

1

/

2 3

The root-to-leaf path1->2represents the number12.

The root-to-leaf path1->3represents the number13.

Return the sum = 12 + 13 =25.

Solution

递归遍历,到叶子节点就累加起来,然后需要用一个变量来记录路径中的值。

Code

class Solution {
public:
int sumNumbers(TreeNode *root) {
int total = 0;
string path;
sumNumbersCore(root, path, total);
return total;
}
void sumNumbersCore(TreeNode* root, string path, int& total) {
if (root != NULL) {
// int 转 char
path.push_back('0' + root->val);
if (root->left != NULL)
sumNumbersCore(root->left, path, total);
if (root->right != NULL)
sumNumbersCore(root->right, path, total);
if (root->left == NULL && root->right == NULL) {
total += stoi(path);
path.pop_back();
}
}
}
};

LeetCode——sum-root-to-leaf-numbers的更多相关文章

  1. LeetCode: Sum Root to Leaf Numbers 解题报告

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

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

  3. [leetcode]Sum Root to Leaf Numbers @ Python

    原题地址:http://oj.leetcode.com/problems/sum-root-to-leaf-numbers/ 题意: Given a binary tree containing di ...

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

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

  6. [Leetcode] Sum root to leaf numbers求根到叶节点的数字之和

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

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

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

  9. LeetCode Sum Root to Leaf Numbers(DFS)

    题意: 给一棵二叉树,每个节点上有一个数字,范围是0-9,将从根到叶子的所有数字作为一个串,求所有串的和. 思路: 普通常规的DFS. /** * Definition for a binary tr ...

  10. leetcode Sum Root to Leaf Numbers(所有路径之和)

    转载请注明来自souldak,微博:@evagle 观察题目给的返回值类型是int,可以断定这棵树的高度不会超过10,所以数据量其实是非常小的.那就直接dfs遍历这棵树,然后到叶子节点的时候将值加到最 ...

随机推荐

  1. Oracle 表管理 约束 索引

    表的约束与完整性: 1.实体完整性 主键唯一性 2.域完整性 不能向number中插入varchar 3.参照完整性 外键 以别的表的字段作为外键,再插入该表时所插入外键的值必须在被参照表中该字段有那 ...

  2. centos7 下 的lamp 的安装原创详细教程

    时间 : 2017-08-03           目标: 基于CENTOS7 安装 LNMP,liunx的安装不做讲解,主要是 NGINX PHP7 MYSQL 的编译安装 第一节    nginx ...

  3. 三维空间直线最近点对hdu4741

    //求两条直线之间的关系(三维) //输入:两条不为点的直线 //输出:相交返回XIANGJIAO和交点p,平行返回PINGXING,共线返回GONGXIAN int LineAndLine(Line ...

  4. 第六课作业——主从复制和sentinel高可用

    第六课时作业 静哥 by 2016.3.21~2016.4.3   [作业描述] 1.配置主从复制,截图看日志 2.配置一个哨兵,一主一从结构,并实现主宕机从接管的过程,截图显示 3.总结哨兵的原理 ...

  5. 好的commit应该长啥样 https://github.com/torvalds/linux/pull/17#issuecomment-5654674

    Git commits历史是如何做到如此清爽的? - 知乎 https://www.zhihu.com/question/61283395/answer/186122300 尤雨溪 前端开发.Java ...

  6. jquery生成二维码图片

    1.插件介绍 jquery.qrcode.min.js插件是jq系列的基于jq,在引入该插件之前要先引入jq.能够在客户端生成矩阵二维码QRCode 的jquery插件 ,使用它可以很方便的在页面上生 ...

  7. Laravel 出现"RuntimeException inEncrypter.php line 43: The only supported ciphers are AES-128-CBC and AES-256-CBC with the correct key lengths."问题的解决办法

    如果输入命令:php artisan key:generate 还是报错 那就要从别的项目里复制一个key到.env中,然后再运行命令:composer update和php artisan key: ...

  8. PAT 1071. 小赌怡情(15) JAVA

    1071. 小赌怡情(15) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue 常言道“小赌怡情”.这是一个很简单的 ...

  9. Log图文详解(Log.v,Log.d,Log.i,Log.w,Log.e)

    android.util.Log常用的方法有以下5个:Log.v() Log.d() Log.i() Log.w() 以及 Log.e() .根据首字母对应VERBOSE,DEBUG,INFO, WA ...

  10. 字符串之strstr

    功能:查找第二个字符串是否存在第一个字符串中. 输入:字符串1,字符串2 返回值:成功返回str1中的位置,失败返回NULL #include <iostream> using names ...