题目:

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. Centos更新yum packet源

    在使用Centos时,常常会遇到使用yum安装某些系统依赖包,特别是第三方软件库(如openstack软件库)时,无法找到包源.因此,需要将Centos的yum源进行更新,扩展,以便可以通过yum的方 ...

  2. 搭建高性能计算环境(九)、应用软件的安装之gaussian 09

    高斯软件一般使用的都是编译好的二进制版,所以解压缩后设置一下环境变量就可以用了. cd /opt tar xvf g09.tar.gz 设置环境变量,添加到/etc/profile文件中,重新登录后生 ...

  3. Lwip的相关资料

    文件资料 Lwip编程指南 Lwip协议栈的设计与实现 分析Lwip

  4. linux下securetty文件

    “/etc/securetty”文件允许你规定“root”用户可以从那个TTY设备登录.登录程序(通常是“/bin/login”)需要读取“/etc/securetty”文件.它的格式是:列出来的tt ...

  5. ADO.NET中ExcuteNonQuery获取存储过程Return返回值

    /// <summary> /// 获取当月用户已投票数量 /// </summary> /// <param name="userId">用户 ...

  6. Winform 打开下载的文件

    private void OpenFile(string filename) { ProcessStartInfo sInfo = new ProcessStartInfo(); sInfo.Wind ...

  7. 高可用工具keepalived学习笔记

    keepalived完全遵守VRRP协议包括竞选机制,至于VRRP是什么这里不说了参考http://wenku.baidu.com/link? url=1UbkmHuQlGECgC90P7zF6u2x ...

  8. android 的通知管理

    1在context里定义通知管理器(NotificationManager) NotificationManager notificationManager = (NotificationManage ...

  9. c#生成随机数示例分享

    c#生成(随机数 http://www.jbxue.com/tags/suijishu.html)的代码. /// 构造随机数 种子 ];             System.Security.Cr ...

  10. javascript绑定时间 含(IE)

    script language = "javascript" type = "text/javascript"> function test(){ win ...