【Leetcode】【Medium】Sum Root to Leaf Numbers (未完成)
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 (未完成)的更多相关文章
- [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 ...
- 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 ...
- 【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 ...
- 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 ...
- [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 ...
- 【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 ...
- 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 ...
- Leetcode#129 Sum Root to Leaf Numbers
原题地址 二叉树的遍历 代码: vector<int> path; int sumNumbers(TreeNode *root) { if (!root) ; ; path.push_ba ...
- LeetCode 129. Sum Root to Leaf Numbers 动态演示
树的数值为[0, 9], 每一条从根到叶子的路径都构成一个整数,(根的数字为首位),求所有构成的所有整数的和 深度优先搜索,通过一个参数累加整数 class Solution { public: vo ...
随机推荐
- 转 $.ajax()方法详解
1.url: 要求为String类型的参数,(默认为当前页地址)发送请求的地址. 2.type: 要求为String类型的参数,请求方式(post或get)默认为get.注意其他http请求方法,例如 ...
- nginx基本配置说明
nginx基本配置与参数说明 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 2 ...
- 阿里云服务器docker搞定镜像
docker的安装可以看前面的文章 这里我的docker已经安装完成了,该搞镜像了:这里的镜像用的是我以前自己搞的,虽然镜像有点大,但是胜在自己搞的,熟悉(熟悉不熟悉鬼知道) 我的镜像放在了阿里云容器 ...
- asp get与post获取的区别
1.HTTP请求格式: <request line> <headers> <blank line> [<request-body>] 在HTTP请求中, ...
- ActiveMQ - 入门指南
首先需要下载ActiveMQ,下面的链接给我们列出了所有版本: http://activemq.apache.org/download-archives.html 每个版本为不同的OS提供了链接: 公 ...
- ,SQL语句关键词以及实例
1.select:功能:查找,语法:select 列名 from 表名(注:可以一次从一个表中查询多个列或者从多个表名中查询资料) 实例:select Name from Table1,返回Table ...
- LeetCode Palidrome Number
class Solution { public: bool isPalindrome(int x) { ) return false; ; int t = x; ; ) { pow *= ; cnt+ ...
- cocos-creator 脚本逻辑-2
1.预制体 1)节点操作 Cc.find(‘node-1’) 获取节点 全局事件 作用于 canvas this.node.destroy() 删除节点(从内存中删除) 添加删除获取节点或组件 let ...
- Django 模型层之多表操作
一.创建模型 实例: 作者表: 拥有字段:姓名(name),性别(sex),该表与书籍表之间为多对多的关系 作者详情表: 拥有字段:地址(addr),手机号(phone),该表与作者表之间为一对一的关 ...
- 牛顿迭代,多项式求逆,除法,开方,exp,ln,求幂
牛顿迭代 若 \[G(F_0(x))\equiv 0(mod\ x^{2^t})\] 牛顿迭代 \[F(x)\equiv F_0(x)-\frac{G(F_0(x))}{G'(F_0(x))}(mod ...