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. 带参数的main函数

    带参数的main函数 int main(int argc,char **argv)  或int main(int argc,char *argv[])  /*解析 依据<C程序设计语言(第二版. ...

  2. 《从零开始学Swift》学习笔记(Day 22)——闭包那些事儿!

    原创文章,欢迎转载.转载请注明:关东升的博客    我给Swift 中的闭包一个定义:闭包是自包含的匿名函数代码块,可以作为表达式.函数参数和函数返回值,闭包表达式的运算结果是一种函数类型. Swif ...

  3. E - I Hate It(基础线段树)

    E - I Hate It Time Limit:3000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Descr ...

  4. Sql注入基础_access注入

    1.access注入攻击片段-联合查询 2.access注入攻击片段-逐字猜解法 3.Access偏移注入(表名和列名猜解成功率不是百分百,猜解不到) access注入攻击片段-联合查询法 判断注入 ...

  5. urlencode rawurlencode htmlspecialchars htmlentities

    w string urlencode ( string $str ) 返回字符串,此字符串中除了 -_. 之外的所有非字母数字字符都将被替换成百分号(%)后跟两位十六进制数,空格则编码为加号(+).此 ...

  6. 类 String、StringBuffer、StringBuilder

    类 String String 类代表字符串.Java 程序中的所有字符串字面值(如 "abc" )都作为此类的实例实现.字符串是常量:它们的值在创建之后不能更改.字符串缓冲区支持 ...

  7. Android的代码都得自己一个个敲一遍吗?

          近期在Android学习中,碰到一个头疼的问题.众所周知Android是一个开发源码的平台,网上有非常多网友分享的关于各种样例的demo,比方扫描二维码,瀑布流等,对于前人已有的成果,我们 ...

  8. 022-Spring Boot 构建微服务实战

    一.概述 二. 2.1.微服务 将原来一个大的系统,拆分成小系统 每个小系统分别开发,测试,维护 2.2.调用方 服务提供方式:rest(http)[restTemplate,httpclient]. ...

  9. 剑指offer 面试61题

    面试61题: 题目:LL今天心情特别好,因为他去买了一副扑克牌,发现里面居然有2个大王,2个小王(一副牌原本是54张^_^)...他随机从中抽出了5张牌,想测测自己的手气,看看能不能抽到顺子,如果抽到 ...

  10. Traverse the dict in Python

    We usually use the following 2 ways to traverse a dict: 1: for d in dic 2: for d in dic.keys() Which ...