题目:

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.

代码:

/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
int sumNumbers(TreeNode* root)
{
return Solution::sumFromRoot2Leaf(root, );
}
static int sumFromRoot2Leaf(TreeNode* root, int upperVal)
{
if ( !root ) return ;
if ( !root->left && !root->right ) return upperVal*+root->val;
return Solution::sumFromRoot2Leaf(root->left, upperVal*+root->val) +
Solution::sumFromRoot2Leaf(root->right, upperVal*+root->val);
}
};

tips:

一开始想自低向上算,后来发现自顶向下算,每次传入上一层的值比较科学。

========================================

第二次过这道题,一次AC了,沿用dfs写法。

/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
int sumNumbers(TreeNode* root)
{
int ret = ;
if ( root ) Solution::dfs(root, , ret);
return ret;
}
static void dfs(TreeNode* root, int sum, int& ret )
{
if ( !root->left && !root->right )
{
sum = sum* + root->val;
ret += sum;
return;
}
if ( root->left ) Solution::dfs(root->left, sum*+root->val, ret);
if ( root->right ) Solution::dfs(root->right, sum*+root->val, ret);
}
};

【Sum Root to Leaf Numbers】cpp的更多相关文章

  1. 【二叉树的递归】07路径组成数字的和【Sum Root to Leaf Numbers】

    ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 给定一个二叉树,节点的值仅限于从0 ...

  2. 【LeetCode】129. Sum Root to Leaf Numbers 解题报告(Python)

    [LeetCode]129. Sum Root to Leaf Numbers 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/pr ...

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

  4. 【LeetCode-面试算法经典-Java实现】【129-Sum Root to Leaf Numbers(全部根到叶子结点组组成的数字相加)】

    [129-Sum Root to Leaf Numbers(全部根到叶子结点组组成的数字相加)] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 Given a bina ...

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

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

  7. LeetCode解题报告—— Sum Root to Leaf Numbers & Surrounded Regions & Single Number II

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

  8. Leetcode之深度优先搜索(DFS)专题-129. 求根到叶子节点数字之和(Sum Root to Leaf Numbers)

    Leetcode之深度优先搜索(DFS)专题-129. 求根到叶子节点数字之和(Sum Root to Leaf Numbers) 深度优先搜索的解题详细介绍,点击 给定一个二叉树,它的每个结点都存放 ...

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

随机推荐

  1. Linux ed命令

    $ ed              <- 激活 ed 命令  a                 <- 告诉 ed 我要编辑新文件  My name is Titan. <- 输入第 ...

  2. 2_JavaScript日期格式化

          第二章 JavaScript 时间格式化 2.1 Ticks 转换为常规日期 2.2 常规日期格式化 <input type="button" value=&qu ...

  3. ping通网关 ping不能外网  DNS无法解析

       ###ping通网关 ping不能外网  DNS无法解析 客户上不了网 DNS解析不了  首先登陆机器 先查看IP  然后看dns是否正常 然后测试ping网关  ping外网 nslookup ...

  4. PHP中数据库的连接

    <?php //1.链接MySQL服务器 $conn = mysql_connect("localhost", "root" , 199452); //2 ...

  5. C#中List〈string〉和string[]数组之间的相互转换

    1,从System.String[]转到List<System.String> System.String[] str={"str","string" ...

  6. jQuery实现跨域访问

    示例: $.ajax({ url: url, crossDomain: true, async: false,dataType:"jsonp" }); 说明:$.ajax()有很多 ...

  7. [Redis] RDB & AOF

    http://my.oschina.net/davehe/blog/174662 rdb - 存在dump.rdb 的二进制文件中 dump 整个db, 数据多的时候,不合适频繁保存,保存的时间间隔应 ...

  8. 将数组之中的省份市区地区ID改成对用中文字符

    数据表数据源的省市区联动: 原始数据: //获取所有学校信息 $school=D('school'); $info=$school->getList(); 数据如下: 1 => array ...

  9. mysql中的 IN和FIND_IN_SET的查询问题

    原来以为mysql可以进行这样的查询select id, list, name from table where 'daodao' IN (list);      (一)注:1. table含有三个字 ...

  10. c#反射机制学习和利用反射获取类型信息

    反射(Reflection)是.NET中的重要机制,通过放射,可以在运行时获得.NET中每一个类型(包括类.结构.委托.接口和枚举等)的成员,包括方法.属性.事件,以及构造函数等.还可以获得每个成员的 ...