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.

C++

/**
* Definition for binary tree
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
int sumNumbers(TreeNode *root){
if(NULL == root) return 0;
int sum = 0;
sum = getSum(root,0);
//getSum2(root,0,sum);
return sum;
}
void getSum2(TreeNode *root, int num, int &sum){
num = num * 10 + root->val;
if(NULL == root->left && NULL == root->right){
sum += num;
return;
}
if(root->left)
getSum2(root->left, num, sum);
if(root->right)
getSum2(root->right, num, sum);
}
int getSum(TreeNode* root, int num){
num = num * 10 + root->val;
if(NULL == root->left && NULL == root->right)
return num;
int sum = 0;
if (NULL != root->left) sum += getSum(root->left,num);
if (NULL != root->right) sum += getSum(root->right,num);
return sum;
}
};

sum-root-to-leaf-numbers leetcode C++的更多相关文章

  1. Sum Root to Leaf Numbers——LeetCode

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

  2. Sum Root to Leaf Numbers leetcode java

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

  3. Sum Root to Leaf Numbers [LeetCode]

    Problem description: http://oj.leetcode.com/problems/sum-root-to-leaf-numbers/ Basic idea: To store ...

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

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

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

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

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

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

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

  10. [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 ...

随机推荐

  1. 为什么在匿名内部类中引用外部对象要加final修饰符

    当所在的方法的形参需要被内部类里面使用时,该形参必须为final. 为什么必须要为final呢? 首先我们知道在内部类编译成功后,它会产生一个class文件,该class文件与外部类并不是同一clas ...

  2. 学习了解PHP中的SeasLog日志扩展

    今天来学习的扩展是和日志相关的一个扩展,对于 PHP 的日志应用来说,除了本身自带的 error_log() . syslog() 之外,在大多数的框架中还会经常见到 monolog 的踪影.当然,我 ...

  3. javascript 自定义事件 发布-订阅 模式 Event

    * javascript自定义事件 var myEvent = document.createEvent("Event"); myEvent.initEvent("myE ...

  4. delete,drop,truncate 区别

    今天看到一篇关于delete.drop.truncate区别的文章,认为写得非常好,转过来. 打比方很形象. delete,drop,truncate 都有删除表的作用,区别在于: 1.delete ...

  5. git 操作 :从远程仓库gitLab上拉取指定分支到本地仓库;git如何利用分支进行多人开发 ;多人合作代码提交实践

    例如:将gitLab 上的dev分支拉取到本地 git checkout -b dev origin/dev 在本地创建分支dev并切换到该分支 git pull origin dev 就可以把git ...

  6. PHP验证

    class yanzhenglei{ /**     * 检查日期格式         * @param string $str 日期格式2015-01-01     * @return bool   ...

  7. P7099-[yLOI2020]灼【数学期望,结论】

    正题 题目链接:https://www.luogu.com.cn/problem/P7099 题目大意 给出\(n\)个坐标轴上的点,\(q\)次询问从某点出发每次等概率向左或者向右一格求到达某个给出 ...

  8. P4494-[HAOI2018]反色游戏【圆方树】

    正题 题目链接:https://www.luogu.com.cn/problem/P4494 题目大意 给出\(n\)个点\(m\)条边的一张无向图,节点有\(0/1\),每条边可以选择是否取反两边的 ...

  9. Java字符串的初始化与比较

    Java字符串的初始化与比较 简单的总结:直接赋值而不是使用new关键字给字符串初始化,在编译时就将String对象放进字符串常量池中:使用new关键字初始化字符串时,是在堆栈区存放变量名和内容:字符 ...

  10. Python | 一键生成九宫格图片

    一键生成九宫格图片 首先我们准备几张图片: 将代码文件放在放置图片的地方,用软件打开: 点击运行,在当前目录下会生成一个文件夹: 打开新生成的文件夹: 打开对应图片的名称文件夹: 如果不想图片被分成9 ...