[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 ...
随机推荐
- jQuery 對table的基本操作
一.鼠标移动到行更换背景色: css样式: .hover{ background-color: #cccc00; } Js脚本: $(document).ready(function () { //鼠 ...
- gulp入门学习
一.gulp简介 gulp是一个自动化构建工具.在开发过工程中,能够使用gulp对项目进行自动构建,大大提高工作效率. 二.安装gulp 在安装gulp之前先要确认已经正确安装了node.js,然后在 ...
- HDU 5139 Formula 卡内存
题目就是求这个 n达到10^7,测试数据组数为10^5 为了防止TLE,一开始把每个n对应的值先求出来,但发现竟然开不了10^7的数组(MLE),然后就意识到这是第一道卡内存的题目... 只能离线做, ...
- IE7append新的元素自动补充完整路径
在IE7下,进行append操作时,会把像<img />的src补成完整路径.对于上传到临时目录的图片,提交到后台要进行路径判断的情形要十分注意.
- Eclipse 运行ant build.xml
在命令行cmd运行mvn clean install,ant compiler,提示上述信息,是因为 maven的这个插件要求jdk1.6,但是本地电脑环境变量jdk版本为1.7.将JAVA_HOME ...
- Html.Action、html.ActionLink与Url.Action的区别
1.html.ActionLink返回的指向指定controller.指定action的超链接标签<a>标签.如果没有指定controller,则默认为本页面对应的Controller. ...
- Session深度探索
什么是Session? web是无状态,这意味着每次页面被回传到服务器时,都重新生成一个web页面类的一个新的实例.众所周知http时无状态的协议.它不能获得客户端的信息.如果用户录入了一些信息,当跳 ...
- linux free命令建检查内存状态
前端时间发现博客服务器物理内存使用过高问题,就是使用linux free命令检查的,这次详细介绍下这个命令. 命 令: free 功能说明:显示内存状态.语 法: free [-bkmotV][-s ...
- mouseover,mouseout和mouseenter,mouseleave
mouseover和mouseout 鼠标指针进入或者离开被选元素或其子元素,都会触发相应事件. 非IE浏览器支持该事件. mouseenter和mouseleave 只有在鼠标指针进入或者离开被选元 ...
- iOS中如何使状态栏与下面的搜索栏或NavigationBar或toolBar颜色一致
在iOS7之后,status bar是透明的(transparent),navigation bars,tab bars,toolbars,search bars 和 scope bars 是半透明的 ...