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 binary tree
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
void solve(TreeNode *root)
{
if(root -> left == NULL && root ->right == NULL){
sum += root->val;
return;
}
if(root->left){
root->left->val += root->val * ;
solve(root->left);
} if(root->right){
root->right->val += root->val * ;
solve(root->right);
}
}
int sumNumbers(TreeNode *root) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
sum = ;
if(root == NULL) return sum;
solve(root);
return sum;
}
int sum ;
};

LeetCode_Sum Root to Leaf Numbers的更多相关文章

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

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

  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解题报告—— 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 ...

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

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

  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. LeetCode OJ--Sum Root to Leaf Numbers

    https://oj.leetcode.com/problems/sum-root-to-leaf-numbers/ 给一棵树,找从根到叶的路径,并把路径上的数相加,求和. 树的深度优先搜索 /** ...

随机推荐

  1. LeetCode_Unique Paths

    A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below). The ...

  2. 磁珠(FB)的选用

    1. 磁珠(FB)的单位是欧姆,而不是亨特,这一点要特别注意.因为磁珠的单位是按照它在某一频率 产生的阻抗来标称的,阻抗的单位也是欧姆.磁珠的 DATASHEET上一般会提供频率和阻抗的特性曲线图,一 ...

  3. Windows脚本 - %~dp0的含义

    含义是:更改当前目录为批处理本身的目录,有些晕吧?不急,我举例 比如你有个批处理a.bat在D:\qq文件夹下  a.bat内容为 cd /d %~dp0 在这里,cd /d %~dp0的意思就是cd ...

  4. 使用CPU的AVX指令

    arch:AVX 很抱歉GCC还不行……有……倒是 但是不是这么写的 我忘记了……官网上有 http://www.oschina.net/news/66980/kreogist-0-9

  5. Ushare应用

    Ushare应用 Openwrt 系统功能强大,主要优势在于其开放性和可扩展性,Openwrt 安装ushare后,可将路由器变身为一个功能强大的家庭upnp流媒体服务器! 打开网上邻居,会显示发现u ...

  6. wordpress提速插件

    auto-remove-googles-url插件,替换前后台国外字体!访问速度有较大提高!可百度搜索auto-remove-googles-url下载,如在wp后台进行插件安装即可

  7. Ubuntu/Linux 笔记应用 为知笔记(支持markdown)

    发现网易云笔记没有Linux,但是为知笔记有Linux版本,且支持markdown格式 sudo add-apt-repository ppa:wiznote-team sudo apt-get up ...

  8. Spring 源码解读 推荐流程

    Spring源代码解析(一):IOC容器:http://www.javaeye.com/topic/86339 Spring源代码解析(二):IoC容器在Web容器中的启动:http://www.ja ...

  9. debian 源

    把下面的源覆盖在/etc/apt/sources.list deb http://http.debian.net/debian wheezy main deb-src http://http.debi ...

  10. Javascript基础form表单

    <!DOCTYPE HTML> <html> <head> <script type="text/javascript" charset= ...