Question:
Given a binary tree containing digits from0-9
only, each root-to-leaf path could represent a number.
An example is the root-to-leaf path1->2->3
which represents the number123
.
Find the total sum of all root-to-leaf numbers.
For example,
1
/ \
2 3
The root-to-leaf path1->2
represents the number12
.
The root-to-leaf path1->3
represents the number13
.
Return the sum = 12 + 13 =25
.
Anwser 1:
/**
* 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 calSum(TreeNode *root, int val, int &sum){
if(root == NULL){
return;
}
if(root->left == NULL && root->right == NULL){
sum += root->val + val * 10;
return;
}
calSum(root->left, val * 10 + root->val, sum);
calSum(root->right, val * 10 + root->val, sum);
}
int sumNumbers(TreeNode *root) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
int sum = 0;
calSum(root, 0, sum);
return sum;
}
};
分享到:
相关推荐
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. An example is the root-to-leaf path 1->2->3 which represents ...
You are given two non-empty linked lists representing two non-negative integers....You may assume the two numbers do not contain any leading zero, except the number 0 itself. java AC版本
numbers 2020-01-22 226 翻转二叉树 2020-01-23 95 不同的二叉搜索树 -变种 96 before 2020-01-24 110 平衡二叉树 -结束之后完成 1227 飞机座位分配概率 2020-01-27 208 字典树 2020-01-28 116 Populating Next ...
双指针算法,python数组双指针算法求和问题LeetCode2sum3sum4sum含代码
文档python数组双指针算法求和问题LeetCode2sum3sum4sum含代码提取方式是百度网盘分享地址
Leetcode two sum java 解法
oj.leetcode题解, 2sum, 运用hashtable解决这到问题,时间复杂度O(N)
自己写的一个完整的程序,包括main函数,在VS上面提交通过,但是放到leetcode上面会出现问题;只是作为一个参考,一起学习学习0.o!解决的问题有:第一:两个链表的最后一个值相加后进位的问题;第二:两个链表的...
LeetCode Roman to Integer解决方案
主要介绍了LeetCode -- Path Sum III分析及实现方法的相关资料,希望通过本文能帮助到大家,需要的朋友可以参考下
The function twoSum should return indices of the two numbers such that they add up to the target, where index1 must be less than index2. Please note that your returned answers (both index1 and index2)...
201 | [Bitwise AND of Numbers Range](https://leetcode.com/problems/bitwise-and-of-numbers-range/) | [C++](./C++/bitwise-and-of-numbers-range.cpp) [Python](./Python/bitwise-and-of-numbers-range.py) | _...
leetcode 530 LeetCode 问题列表,包括锁定的问题。 [1028] ...Numbers Easy (45.80 %) [1021] Remove Outermost Parentheses Easy (80.59 %) [1020] Number of Enclaves Medium (54.07 %) [1019] Next G
刷LeetCode刷LeetCode刷LeetCode刷LeetCode刷LeetCode
leetcode 轮廓 1_count_and_say.cpp - super_ugly_number.cpp - Detect_Pattern.cpp - degree_of_array.cpp - 键盘.cpp - 2Sum_Data_Structure_Design.cpp - shuffle_array.cpp - permutations.cpp - kth_missing_...
二叉搜索树迭代器-Sum Root to Leaf Numbers 广度优先搜索 -二叉树级顺序遍历-二叉树级顺序遍历II - 二叉树之字形层序遍历-课程安排-课程表二-岛屿数量-太平洋大西洋水流-周边地区-对称树- 克隆图- 字梯-字梯II 回溯...
颜色分类leetcode 植物叶片感染检测 这是我们在 Inderscience 期刊 (IJSHC) 上提交审查的工作的源代码。 存储库包含代码以及用于模拟的数据。 数据集取自 Sharada Mohanty 的 PlanVillage 数据集 原始目录中存在不同...
Automatically sync your leetcode solutions to your github
leetcode 2SUM 使用以前的地图最快的 2Sum O(N) 使用 unordered_map 比 map 快 以前的地图 là gì ? 上一张地图 đơn giản là 地图 nhưng thay vì ta cần một vòng for để khởi tạo 地图 thì ta khởi...
答案LeetCode_1_TwoSum LeetCode 问题:给定一个整数数组,找出两个数字,使它们相加为特定的目标数字。 函数 twoSum 应该返回两个数字的索引,使它们相加为目标,其中 index1 必须小于 index2。 请注意,您返回的...