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 ...
随机推荐
- ubuntu下读取数据库中文乱码解决
请按如下配置myqsl.cnf (/etc/mysql/mysql.conf.d/mysql.cnf ),然后重启mysql服务,对于web程序,你可以把web所有编码都搞成utf-8[client] ...
- Spring MVC无法获取ajax POST的参数和值
一.怎么会这个样子 很简单的一个想法,ajax以POST的方式提交一个表单,Spring MVC解析.然而一次次的打印null折磨了我整整一天…… 最后的解决现在看来是很明显的问题,“只是当时已惘然” ...
- DevExpress所有的窗体,使用同一款皮肤
https://www.devexpress.com/Support/Center/Question/Details/K18516 To accomplish your task, please ex ...
- [51NOD1537] 分解(递推,矩阵快速幂)
题目链接:http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1537 思路:一开始用二项式拆了一下,发现这个式子的形式总能变成 ...
- python_way day14 CSS
python_way day14 CSS 层叠样式表 一.CSS作用域: 二.css标签选择器 三.css样式 一.css作用域: 基本用法:style="样式" <body ...
- hdu 5950 Recursive sequence 矩阵快速幂
Recursive sequence Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Other ...
- 《易货》Alpha版本测试报告
一.测试计划 功能需求编号 功能需求名称 功能需求描述 测试计划 1 用户注册 每一个想要发布商品或者需要购买商品的用户都需要注册一个账号 √ 2 用户登录 已经拥有账号的用户登录 √ 3 密码修改 ...
- Thinking in Java 笔记初衷
当读到第十章的时候,发现之前读过的内容很难准确的记忆起来,这样给后续的阅读带来了一定的困惑,而之前也没有做适当的可读性的复习笔记,所以发现重新找后续阅读需要的知识点时,是有一定的困难的. 同时在知乎今 ...
- 08 高效的SQL
编写高效 SQL 需要以下知识 有关所查询内容的物理组织的知识 数据库能做什么的知识, 例如: 如果你不知道跳跃扫描索引及其用途, 那么你可能会看着模式说”索引丢了” SQL 所有错综复杂的知识 对目 ...
- web设计经验<六>令网站看起来不专业的10个设计误区
不管你是不是一个羽翼未丰企业的领导,专业的网站能为你带来的东西比你想象的多很多.退一万步来说,“考虑到我们是一个小厂”,粗糙的网站也许能被用户理解,但是不一定能接受.每天大家所浏览的大量的网站,已经从 ...