Sum Root to Leaf Numbers [LeetCode]
Problem description: http://oj.leetcode.com/problems/sum-root-to-leaf-numbers/
Basic idea: To store the num vector in every node of tree by starting from leaf, the go up util to root.
class Solution {
public:
vector<vector<int>> subNumbers(TreeNode *root) {
vector<vector<int>> sums;
if(root == NULL)
return sums;
if(root->left == NULL && root->right == NULL){
vector<int> seq;
seq.push_back(root->val);
sums.push_back(seq);
return sums;
}
vector<vector<int>> left_sums = subNumbers(root -> left);
for(auto item: left_sums) {
item.insert(item.begin(), root->val);
sums.push_back(item);
}
vector<vector<int>> right_sums = subNumbers(root -> right);
for(auto item: right_sums) {
item.insert(item.begin(), root->val);
sums.push_back(item);
}
return sums;
}
int pow10(int n) {
int ret = ;
for(int i = ; i < n; i++)
ret = ret * ;
return ret;
}
int sumNumbers(TreeNode *root) {
// Note: The Solution object is instantiated only once and is reused by each test case.
int sum = ;
vector<vector<int>> sums = subNumbers(root);
for(auto v : sums){
int tmp_sum = ;
for(int i = v.size() - ; i >= ; i -- ) {
tmp_sum += v[i] * pow10(v.size() - - i);
}
sum += tmp_sum;
}
return sum;
}
};
Sum Root to Leaf Numbers [LeetCode]的更多相关文章
- 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 ...
- 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 ...
- Leetcode之深度优先搜索(DFS)专题-129. 求根到叶子节点数字之和(Sum Root to Leaf Numbers)
Leetcode之深度优先搜索(DFS)专题-129. 求根到叶子节点数字之和(Sum Root to Leaf Numbers) 深度优先搜索的解题详细介绍,点击 给定一个二叉树,它的每个结点都存放 ...
- 【LeetCode】129. Sum Root to Leaf Numbers 解题报告(Python)
[LeetCode]129. Sum Root to Leaf Numbers 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/pr ...
- 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 ...
- 【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 ...
- 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 ...
- 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 ...
- [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 ...
随机推荐
- nuget.exe the application could not be started
http://stackoverflow.com/questions/5730412/error-when-running-the-nuget-exe-command Ok, so this turn ...
- mysql启动关闭
RedHat Linux (Fedora Core/Cent OS) 1.启动:/etc/init.d/mysqld start2.停止:/etc/init.d/mysqld stop3.重启:/et ...
- servlet&jsp高级:第三部分
声明:原创作品,转载时请注明文章来自SAP师太技术博客( 博/客/园www.cnblogs.com):www.cnblogs.com/jiangzhengjun,并以超链接形式标明文章原始出处,否则将 ...
- python_way,day4 内置函数(callable,chr,随机验证码,ord),装饰器
python_way,day4 1.内置函数 - 下 制作一个随机验证码 2.装饰器 1.内置函数 - 下 callable() #对象能否被调用 chr() #10进制数字对应的ascii码表中的内 ...
- jquery的隐式类型转换
jquery的选择器想用变量来传,然后就纠结怎么写引号的问题??? 当时脑子就犯轴了,这个我要是传变量怎么写引号啊,我要是在最外层在加一层引号就不对了,就没法识别变量了,不加反而对了 那就用conso ...
- netbeans环境的建立
这是一个简单的实验,熟悉NetBeans的IDE环境的开发 首先下载一个NetBeans,可以在官网上下https://netbeans.org/downloads/index.html 要装NetB ...
- ORACLE SQL 分组
select max(cost),suppliercode from tel_bill where period = '2014005' group by suppliercode;select * ...
- poj2194Stacking Cylinders
链接 可以根据反余弦和反正切算出角a和b的值, 然后向量旋转就可以了,图中的状态旋转rotate((2,0),a+b) 反状态把角度反过来,点取(-2,0)即可. 不知道是不是理解错了,题意写着两圆 ...
- CSS重置代码和常用公共代码
发的发生的发生法士大夫撒打发士大夫
- 论APP测试中黑盒测试方案的重要性?
运筹帷幄之中,决胜千里之外.古人足不出户,通过正确的部署就能决定千里之外战争的胜利!而于测试人员而言,制定正确的测试方案,就是日后测试就是是否顺利的决定性因素. 在整个测试过程中,对测试人员.资源以及 ...