[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.
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.
问题:给定一个二叉树,树的每个节点都只包含一个数字 0-9,将根节点到叶子节点路径上的元素值组合成一个整数,求所有整数和。
这是一道深度遍历的应用。深度遍历二叉树一般是递归遍历,或者借助栈遍历。每到达一个叶子节点,都需要重新遍历一次根节点到该叶子节点路径的值,借组栈遍历可以实现满足这个需要。
#define null_symble -2147483648
int sumNumbers(TreeNode* root) {
if(root == NULL){
return ;
}
list<TreeNode*> stackt;
stackt.push_back(root);
int sum = ;
map<TreeNode*, int> node_val;
while(stackt.size() > ){
TreeNode* node = stackt.back();
if(node->val != null_symble){
node_val[node] = node->val;
node->val = null_symble;
}
if (node->left != NULL && node->left->val != null_symble){
stackt.push_back(node->left);
continue;
}
if(node->right != NULL && node->right->val != null_symble){
stackt.push_back(node->right);
continue;
}
if(node->left == NULL && node->right == NULL){
string str = "";
list<TreeNode*>::iterator l_iter;
for(l_iter = stackt.begin(); l_iter != stackt.end(); l_iter++){
str += to_string(node_val[*l_iter]);
}
sum += stoi(str);
}
stackt.pop_back();
}
return sum;
}
[LeetCode] 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] 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 (DFS)
https://leetcode.com/problems/sum-root-to-leaf-numbers/ Given a binary tree containing digits from 0 ...
- 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 ----- 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
原题地址 二叉树的遍历 代码: 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: 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 ...
随机推荐
- [ES6] ... spread operator
var parts = ['shoulders', 'knees']; var lyrics = ['head', ...parts, 'and', 'toes']; // ["head&q ...
- 使用单例模式实现自己的HttpClient工具类
引子 在Android开发中我们经常会用到网络连接功能与服务器进行数据的交互,为此Android的SDK提供了Apache的HttpClient 来方便我们使用各种Http服务.你可以把HttpCli ...
- Blade和其他构建工具有什么不同
大部分人都至少接触过不止一种构建工具,比如make,autotools.而我们开发了Blade,为什么那么多现成的工具不用,而又再造了一个轮子,相对于传统的make等工具,Blade的好处在又哪里呢? ...
- 关于Adobe Flash 11.3 引起的火狐使用问题
Adobe Flash 更新到11.3之后,为火狐引入Flash沙盒安全模式,但同时,又造成了部分兼容性问题,导致 Windows vista及 Windows 7上部分火狐崩溃,并致使一些使用Fla ...
- 重复造轮子感悟 – XLinq性能提升心得
曾经的两座大山 1.EF 刚接触linq那段时间,感觉这家伙好神奇,语法好优美,好厉害.后来经历了EF一些不如意的地方,就想去弥补,既然想弥补,就必须去了解原理.最开始甚至很长一段时间都搞不懂IQue ...
- zepto源码研究 - zepto.js-4(常用的工具)
$.each: /** * 以集合每一个元素作为上下文,来执行回调函数 * @param elements * @param callback * @returns {*} */ $.each = f ...
- cos实现文件上传--推荐
1.导包 2.核心类:MultipartRequest MultipartRequest是request的包装类 参数1:request 参数2:保存路径 参数3:上传文件大小,默认为1M 参数4:编 ...
- STL-空间配置器(allocator)
STL的空间配置器作为STL六大部件的重要组成部分,它总是隐藏在一切组件的背后.它主要负责动态空间的分配.释放等管理工作.整个STL的操作对象(所有的数值)都存放在容器之内,而容器一定需要配置空间以置 ...
- DedeCMS中最重要的四类表
栏目(类别): dede_arctype (dede数据库设计者认为:不管你是存放什么样的数据(软件,商品,电影..)都应该属于某个栏目(类型)) 内容主表:dede_archives (织梦数据库的 ...
- python ciscolib模块
python的第三方模块ciscolib可以登录交换机执行相关命令 ciscolib下载地址: https://github.com/nickpegg/ciscolib 常见用法如下: switch ...