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 ...
随机推荐
- spoj 394
每段可以连续的串的可能性是个Fibonacci数列 但是直接dp更好吧~~ #include <cstdio> #include <cstring> using names ...
- Linux rm命令
rm可以用来删除文件和文件夹. rm --help Usage: rm [OPTION]... FILE... Remove (unlink) the FILE(s). -f, --force ...
- UNITY3D使用NGUI制作自适应UI的总结
原地址:http://www.cnitblog.com/updraft/archive/2013/11/12/88801.html 制作自适应的几个方法1. 使用 UIROOT 里设置自定义高度的方法 ...
- POJ1151+线段树+扫描线
/* 线段树+扫描线+离散化 求多个矩形的面积 */ #include<stdio.h> #include<string.h> #include<stdlib.h> ...
- SaaS系列介绍之五: 我国SaaS市场分析
1 我国SaaS市场现状 我国SaaS从ASP年代发展到今天,也有不少时间了.我国是个十几亿人的大国,国情复杂,各地贫富不均,发展不平衡.信息系统建设也是各树一帜,各地为王.特别是占有大量用户的中小企 ...
- Android 遍历sdcard中指定文件夹下的图片(jpg,jpeg,png)
File scanner5Directory = new File(Environment.getExternalStorageDirectory().getPath() + "/scann ...
- Android EditText属性
1.EditText输入的文字为密码形式的设置 (1)通过.xml里设置: 把该EditText设为:android:password="true" // 以”.”形式显示文本 ( ...
- Winform 控件使用集锦
DataGridView中checkbox的值读取问题.checkbox选中之后,在CellClick事件中通过Value是读取不到值的,在当前单元格变为另一个单元格之前,它的值不会写到DataGri ...
- python学习笔记三--字典的使用
一.基本使用: 1. 赋值:{key:value} 1.1 与列表相同处:会改变索引(键)相关联的值的改变 1.2 与列表不同处:不用考虑值的长度,而列表是有序的需要考虑末尾偏移量,超过末尾偏移量的会 ...
- 2.2linux内核移植简介
1,编译linux3.5出错 root@phone-desktop:/opt/FriendlyARM/tiny4412/Linux/linux-3.5# makescripts/kconfig/con ...