题目:

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.

分类:Tree 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) {
return DFS(root,);
} int DFS(TreeNode* root, int sum)
{
if(!root)
return ;
int newSum = sum * + root->val;
if(!root->left && !root->right)
return newSum;
return DFS(root->left,newSum) + DFS(root->right,newSum);
}
};

 

[LeetCode129]Sum Root to Leaf Numbers的更多相关文章

  1. Leetcode129. Sum Root to Leaf Numbers求根到叶子节点数字之和

    给定一个二叉树,它的每个结点都存放一个 0-9 的数字,每条从根到叶子节点的路径都代表一个数字. 例如,从根到叶子节点路径 1->2->3 代表数字 123. 计算从根到叶子节点生成的所有 ...

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

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

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

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

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

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

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

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

  8. [Swift]LeetCode129. 求根到叶子节点数字之和 | Sum Root to Leaf Numbers

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

  9. LeetCode129:Sum Root to Leaf Numbers

    题目: Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a nu ...

随机推荐

  1. Mono和Jexus并且部署ASP.NET MVC3、4、5和WebApi

    Linux(CentOS 6.7)下配置Mono和Jexus并且部署ASP.NET MVC3.4.5和WebApi(跨平台) 1.开篇说明 a. 首先我在写这篇博客之前,已经在自己本地配置了mono和 ...

  2. PS CC 2014 把一个图层输出为文件的方法

    近期在设计一个Qt控件,须要获得一个圆饼的图片,在用PS绘制后发现保存的时候总是会带着背景,用PNG格式保存之后背景依旧存在.仅仅是变成了透明的.刚才在Google上查到了仅仅保存单一图层而全然没有背 ...

  3. 基于Grunt的版本号构建系统新手教程

    作者:zhanhailiang 日期:2014-10-12 1. 安装nodejs,npm,grunt-cli.參见<Windows环境下安装nodejs+npm+grunt-cli工具> ...

  4. 使用Spring的JAVA Mail支持简化邮件发送(转)

    闲来无事,翻看<Spring in Action>,发现Spring集成了对JAVA Mail的支持,有点小激动的看了一遍,嗯,话说真的简单了很多. Spring的邮件发送的核心是Mail ...

  5. hdu1151 Air Raid,DAG图的最小路径覆盖

    点击打开链接 有向无环图的最小路径覆盖 = 顶点数- 最大匹配 #include <queue> #include <cstdio> #include <cstring& ...

  6. 如何解决This system is not registered with RHN.

    今天我必须写下这篇文章,由于在我刚刚接触到Linux下安装oracle时碰到了Linux中少xscreensaver.rpm包自己弄了非常久.最后还是被一个大哥帮我攻克了:仅仅能说非常的感谢你! 我试 ...

  7. HDU 5074-Hatsune Miku(DP)

    Hatsune Miku Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others) T ...

  8. android maven eclipse里面新建mavenprojectThe desired archetype does not exist

    这个问题头疼死我了 又一次配置下你看我的教程 watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvY2hlbmFpbmkxMTk=/font/5a6L5L2T/f ...

  9. VC中Tab control的用法

    1. 新建一个MFC工程, 取名MyTab, 选择Dialog based, 然后Finish. 2. 删除对话框上默认添加的三个控件. 添加Tab Control控件并在Property属性中设置I ...

  10. Javascript Base64编码与解码

    原文:[转]Javascript Base64编码与解码 <html> <head> <META HTTP-EQUIV="MSThemeCompatible&q ...