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-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
.
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
void travel(vector<vector<int> >& nums, vector<int>& load, TreeNode* root) {
if(!(root->left) && !(root->right)) {
nums.push_back(load);
return;
} if(root->left) {
load.push_back(root->left->val);
travel(nums, load, root->left);
load.pop_back();
}
if(root->right) {
load.push_back(root->right->val);
travel(nums, load, root->right);
load.pop_back();
}
}
int sumNumbers(TreeNode* root) {
if(root == NULL) return ; vector<vector<int> > nums;
vector<int> load;
int res = ; load.push_back(root->val);
travel(nums, load, root); for(int i=; i<nums.size(); ++i) {
int rhs = ;
for(int j=; j<nums[i].size(); ++j) {
rhs = rhs * + nums[i][j];
}
res += rhs;
}
return res;
}
};
leetcode@ [129] Sum Root to Leaf Numbers (DFS)的更多相关文章
- [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 ----- 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 ...
- 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 ...
- [LeetCode]129. Sum Root to Leaf Numbers路径数字求和
DFS的标准形式 用一个String记录路径,最后判断到叶子时加到结果上. int res = 0; public int sumNumbers(TreeNode root) { if (root== ...
- 【LeetCode】129. Sum Root to Leaf Numbers 解题报告(Python)
[LeetCode]129. Sum Root to Leaf Numbers 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/pr ...
- 【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 ...
随机推荐
- uva 10827
与108类似 多加了两层循环 水过 #include <iostream> #include <cstring> #include <cstdio> #includ ...
- 互斥锁Mutex与信号量Semaphore的区别
转自互斥锁Mutex与信号量Semaphore的区别 多线程编程中,常常会遇到这两个概念:Mutex和Semaphore,两者之间区别如下: 有人做过如下类比: Mutex是一把钥匙,一个人拿了就可进 ...
- STM32移植UCGUI3.90笔记
在MDK环境下,终于将3.90版本的UCGUI移植到STM32下了,在网上看到的都是例程代码,很少看到有关于在STM32下移植UCGUI的教程方法,为了方便大家,特写此移植方法,大家可以借鉴(有错误之 ...
- 1964-NP
描述 Problems in Computer Science are often classified as belonging to a certain class of problems (e. ...
- codeforces #310 div1 E
算得上是比较水的E题了吧,自己想了想写了写居然1A了 对于这道题,我们很容易想到对于原图的一个边双,定向后任意两点间一定可达 那么我们可以求出原图的边双并将每个边双缩成一个点 那么原图就变成了无环的无 ...
- H264/AVC视频解码时AVC1和H264的区别
AVC1与H264的区别 http://blog.csdn.net/qiuchangyong/article/details/6660253 H.264 Video Types The followi ...
- 两页pdf打印为一页,并且放大(打印英文pdf常用)
多很英文书籍都是小书,若我们直接打印它的pdf会很厚,比如我要打印一本 thinking in C++,就要800+页.不如把两页打成一页.但是打成一页之后又太小了,需要放大.具体方法如下: 前提 ...
- DSP6455 DSP/BIOS中断配置问题(是否需要ECM-事件组合以及实例)
2013-06-20 21:08:48 中断的配置有两种常用的方式: 一是通过CSL提供的API进行配置,这种方法相对DSP/BIOS偏底层,也比较麻烦:这种方法要求对中断系统的工作方式很清楚. 二是 ...
- 安装universal-ctags
universal-ctags是exuberant-ctags的替代者,相比gtags可以解析更多的语言,但是其他方面比如操作,数据库的组织方式等就不够好.需要自己编译安装 先用 git clone ...
- [转] Android自动化测试之使用java调用monkeyrunner(五)
Android自动化测试之使用java调用monkeyrunner 众所周知,一般情况下我们使用android中的monkeyrunner进行自动化测试时,使用的是python语言来写测试脚本.不过, ...