【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 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.
每到达一个根节点,就代表一个路径访问完成,将和加入总和。
解法一:
树结构用递归是最容易的,每递归一层,上层的部分和需要乘以10在做加法。
/**
* Definition for binary tree
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
int sumNumbers(TreeNode *root) {
int Sum = ;
Helper(root, , Sum);
return Sum;
}
void Helper(TreeNode* root, int partSum, int& Sum)
{
if(root == NULL)
return;
else if(root->left == NULL && root->right == NULL) //add this path
Sum += (*partSum+root->val);
else
{
Helper(root->left, *partSum+root->val, Sum);
Helper(root->right, *partSum+root->val, Sum);
}
}
};

解法二:
非递归方法,使用栈存放当前的路径进行深度搜索,并设置部分和记录栈中的数值。
结合进栈与出栈进行部分和调整:
进栈:部分和*10+当前值
出栈:(部分和-当前值)/10
如果遍历到叶节点,则将部分和加入总和。
/**
* 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:
int sumNumbers(TreeNode* root) {
if(root == NULL)
return ;
int ret = ;
int cur = ;
stack<TreeNode*> stk;
unordered_map<TreeNode*, bool> visited;
stk.push(root);
visited[root] = true;
cur += root->val;
while(!stk.empty())
{
TreeNode* top = stk.top();
if(top->left != NULL && visited[top->left] == false)
{
stk.push(top->left);
visited[top->left] = true;
cur = cur* + top->left->val;
continue;
}
if(top->right != NULL && visited[top->right] == false)
{
stk.push(top->right);
visited[top->right] = true;
cur = cur* + top->right->val;
continue;
}
if(top->left == NULL && top->right == NULL)
{
ret += cur;
}
stk.pop();
cur = (cur - top->val) / ;
}
return ret;
}
};

【LeetCode】129. Sum Root to Leaf Numbers (2 solutions)的更多相关文章
- 【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 OJ 129. Sum Root to Leaf Numbers
题目 Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a num ...
- 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 ...
- [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 ...
- 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 ...
随机推荐
- 在Mac OS X上配置Apache2
转载:http://www.cnblogs.com/yuanyq/p/3435022.html#2821339 最近一段时间在开发面向移动设备的网页,而且是静态网页.所以很需要一个HTTP服务器,简单 ...
- OpenCV学习(33) 轮廓的特征矩Moment
在OpenCV中,可以很方便的计算多边形区域的3阶特征矩,opencv中的矩主要包括以下几种:空间矩,中心矩和中心归一化矩. class Moments { public: ...... // 空间矩 ...
- 判断大小端的方法(java和c++)
首先我们给出大小端的定义: 小端:较高的有效字节存放在较高的的存储器地址,较低的有效字节存放在较低的存储器地址. 大端:较高的有效字节存放在较低的存储器地址,较低的有效字节存放在较高的存储器地址. 将 ...
- JavaScript使用技巧精萃
(一).确认删除用法: 1. BtnDel.Attributes.Add("onclick","return confirm('"+"确认删除?& ...
- 还原JavaScript的真实历史~
问题 ============ JavaScript真的继承自Cmm吗? JavaScript与Java有多少关系? JavaScirpt最初的设计是怎样的?在许多资料,JavaScript的语源被追 ...
- Java基础(六):继承
1.继承的概念: 继承是java面向对象编程技术的一块基石,因为它允许创建分等级层次的类.继承就是子类继承父类的特征和行为,使得子类对象(实例)具有父类的实例域和方法,或子类从父类继承方法,使得子类具 ...
- SpringApplicationConfiguration 这个不能用 解决方案
使用的test包的版本号要与spring的一致,避免jar包依赖冲突 直接用注解 @RunWith(SpringRunner.class)@SpringBootTest @SpringApplicat ...
- [Backbone] Verying Views
Below we have our AppointmentsView instance rendering and then taking the rendered HTML and insertin ...
- 【Nodejs】使用put方式向后端查询数据并在页面显示
前端代码: <!DOCTYPE html> <html lang="utf-8"> <meta http-equiv="Content-Ty ...
- 如何使用FF的Firebug组件中的net工具查看页面元素加载消耗时间
1.安装FF的Firebug组件:点击FF的Tools的Add-ons菜单,输入Firebug关键字,并选择合适的版本Install. 2.安装完毕后地址栏右边会出现一个小虫图标,右边还有一个下拉箭头 ...