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.

解题思路1:

以按层遍历的思维,当要深入下一层时,将当前层累加的value值乘以10,并带入下一层继续运算,直到运算到叶子结点。之后累加所有叶子结点。

使用递归很容易实现:

 /**
* 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 (!root)
return ; if (!root->left && !root->right)
return root->val;
else
return sumLeaf(root->left, root->val) +
sumLeaf(root->right, root->val);
} int sumLeaf(TreeNode *node, int cur_sum) {
if (!node)
return ; if (!node->left & !node->right)
return cur_sum * + node->val;
else
return sumLeaf(node->left, cur_sum * + node->val) +
sumLeaf(node->right, cur_sum * + node->val);
}
};

解题思路2:

递归的使用,造成程序运行慢。为了不使用递归可以按照二叉树先序遍历的方法。

代码:

【Leetcode】【Medium】Sum Root to Leaf Numbers (未完成)的更多相关文章

  1. [LeetCode] 129. Sum Root to Leaf Numbers 求根到叶节点数字之和

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

  2. leetcode@ [129] Sum Root to Leaf Numbers (DFS)

    https://leetcode.com/problems/sum-root-to-leaf-numbers/ Given a binary tree containing digits from 0 ...

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

  4. leetcode 129. Sum Root to Leaf Numbers ----- java

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

  5. [LeetCode] 129. Sum Root to Leaf Numbers 解题思路

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

  6. 【LeetCode】Sum Root to Leaf Numbers

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

  7. Java for LeetCode 129 Sum Root to Leaf Numbers

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

  8. Leetcode#129 Sum Root to Leaf Numbers

    原题地址 二叉树的遍历 代码: vector<int> path; int sumNumbers(TreeNode *root) { if (!root) ; ; path.push_ba ...

  9. LeetCode 129. Sum Root to Leaf Numbers 动态演示

    树的数值为[0, 9], 每一条从根到叶子的路径都构成一个整数,(根的数字为首位),求所有构成的所有整数的和 深度优先搜索,通过一个参数累加整数 class Solution { public: vo ...

随机推荐

  1. Java 网络通信相关

    http://m.blog.csdn.net/xiaojin21cen/article/details/78587541 越下面越底层 , 最后面的都是框架 , 下面的是 编程语言提供的库的 NIO ...

  2. jacvascript 保留小数点

    //四舍五入保留2位小数(若第二位小数为0,则保留一位小数) function keepTwoDecimal(num) {  var result = parseFloat(num);  if (is ...

  3. MySQL对结果进行排序order by

    order by {col_name | expr | position} [ASC | DESC] 查询结果     排序条件的顺序  决定   排序条件   的优先级 如果同一条件下值相等,那么启 ...

  4. python中文分词工具——结巴分词

    传送门: http://www.iteye.com/news/26184-jieba

  5. 破解b站极验验证码

    这就是极验验证码,通过拖动滑块移动拼图来验证.我们观察到点击滑块时拼图才会出现,所以我们可以在点击滑块之前截取图像,点击滑块再截取一次图像,将前后两次图像做比较就可以找到图片改动的位置.获得位置后,我 ...

  6. rest webapi 返回数据

    webapi可以直接返回一个对象,也可以返回json 一.返回一个对象例子 [System.Web.Mvc.AllowAnonymous] [System.Web.Http.HttpGet] publ ...

  7. TortoiseGit记住用户名&密码

    配置并安装好git之后鼠标右键: 在全局配置文件末尾添加一行: [credential] helper = store *主意保存时以utf-8格式保存,否则中文可能会乱码,这样下次只需输入一次用户名 ...

  8. centos下mongodb安装

    安装说明: 系统环境:Centos-6.5 安装软件:mongodb-linux-x86_64-2.4.9.tgz 下载地址:http://www.mongodb.org/downloads 上传位置 ...

  9. 微信小程序开发框架整理

    目前除了原生的微信小程序开发外,各大厂商陆续造了自己的开发框架,现整理如下: WePY 腾讯官方开源的小程序组件化开发框架,目前有15K+Star ,一直在更新着,社区活跃,掉坑能快速的找到方法爬出来 ...

  10. 【转】.net MVC 生命周期

    对于Asp.net MVC,我对它的生命周期还是兴趣很浓,于是提出两个问题: 一个HTTP请求从IIS移交到Asp.net运行时,Asp.net MVC是在什么时机获得了控制权并对请求进行处理呢?处理 ...