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. eclipse4.2+安装modelgoon插件,该插件支持在eclipse直接依据java文件生产类图

    安装条件: 1. 确保JDK环境OK 2.该插件安装是基于eclipse kepler(4.2) (并非表示其它版本号不能安装,仅仅是博主仅仅在4.2版本号上測试了.预计4.3版本号还是支持的,可是3 ...

  2. iOS学习笔记(六)——ViewController

    ViewController是iOS应用程序中重要的部分,是应用程序数据和视图之间的重要桥梁,ViewController管理应用中的众多视图.iOS的SDK中提供很多原生ViewController ...

  3. 《从零开始学Swift》学习笔记(Day 35)——会使用下标吗?

    原创文章,欢迎转载.转载请注明:关东升的博客 看下面的示例代码是不是使用过: var studentList: String[] = ["张三","李四",&q ...

  4. 《从零开始学Swift》学习笔记(Day 30)——选择类还是结构体呢?

    原创文章,欢迎转载.转载请注明:关东升的博客 类和结构体非常相似,很多情况下没有区别.如果你是设计人员在进行系统设计时候,是将某种类型设计成为类还是结构体? 类和结构体异同: 类和结构体都有如下功能: ...

  5. 注册Asp4.0到iis

  6. 前端 为什么我选择用框架而不是Jquery

    对于很多习惯用Jquery的前端甚至后端,都很不解,为什么不用Jquery而是框架.觉得框架学起来麻烦,成本高,今天我以我浅薄的知识来总结一下为什么前台开发选择用框架: 前台开发,主要的性能是卡在回流 ...

  7. JS和C# 里的闭包及闭包在事件中的使用

    在Javascript世界里,无所不用闭包及自定义事件, 自定义事件其实也是事先定义好一种规则 ,当触发者被响应后执行的一段回调.下面看个例子 function dothing(callBack){ ...

  8. python字符串拼接相关

    #字符串拼接#str.join(元组.列表.字典.字符串) 之后生成的只能是字符串.str = "-";seq = ("a", "b", & ...

  9. h5-localStorage实现缓存ajax请求数据

    使用背景:要实现每次鼠标hover“能力雷达”,则显示能力雷达图(通过ajax请求数据实现雷达图数据显示),所以每次hover都去请求ajax会影响性能,因此这里要用到本地缓存. 实现: 此处是通过传 ...

  10. 2015-02-08——js笔记

    示例1: 关于事件对象 MSIE:window.event,  cancelBubble,  returnValue,  srcElement, button(鼠标按键,1,4,2,左中右) W3C: ...