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)的更多相关文章

  1. 【LeetCode】129. Sum Root to Leaf Numbers 解题报告(Python)

    [LeetCode]129. Sum Root to Leaf Numbers 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/pr ...

  2. 【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 ...

  3. 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 ...

  4. 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 ...

  5. [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 ...

  6. 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 ...

  7. 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 ...

  8. [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 ...

  9. 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 ...

随机推荐

  1. 《阿里巴巴JAVA开发手册》里面写超过三张表禁止join这是为什么?

    分库分页.应用里做join 多表join性能很差 参考: 1.https://www.zhihu.com/question/56236190

  2. OTL翻译(8) -- otl_long_string/otl_long_unicode_string类

    otl_long_string/olt_long_unicode_string 这两个类主要用来处理大对象数据.从OTL4.0版本开始,otl_long_string还可以处理任何类型的RAW/BIA ...

  3. atoi(),atof等函数的实现

    atoi()函数的功能:将字符串转换成整型数:atoi()会扫描参数nptr字符串,跳过前面的空格字符,直到遇上数字或正负号才开始做转换,而再遇到非数字或字符串时('\0')才结束转化,并将结果返回( ...

  4. Math Number 数值类 包装类 数学计算 MD

    Markdown版本笔记 我的GitHub首页 我的博客 我的微信 我的邮箱 MyAndroidBlogs baiqiantao baiqiantao bqt20094 baiqiantao@sina ...

  5. Vue路由scrollBehavior滚动行为控制锚点

    使用前端路由,当切换到新路由时,想要页面滚到顶部,或者是保持原先的滚动位置,就像重新加载页面那样. vue-router 能做到,而且更好,它让你可以自定义路由切换时页面如何滚动. 注意: 这个功能只 ...

  6. JavaScript事件代理和事件委托

    一.概述: 那什么叫事件委托呢?它还有一个名字叫事件代理,JavaScript高级程序设计上讲:事件委托就是利用事件冒泡,只指定一个事件处理程序,就可以管理某一类型的所有事件.那这是什么意思呢?网上的 ...

  7. Metronic V1.5.2 Responsive Admin Dashboard Template build with Twitter Bootstrap 3.0

    Template Name: Metronic - Responsive Admin Dashboard Template build with Twitter Bootstrap 3.0 Versi ...

  8. mybatis结果的组装(springboot)

    文主要解答一个问题,即如果bean没有setter,而且属性不是public的,mybatis的自动组装是否可以赋值成功的问题. 查询调用过程 DefaultSqlSession.selectList ...

  9. Foreda8上安装Ant1.9.2

    Ant在Win上安装很简单,解压拷贝+设置Ant_Home,在Linux上差不多也是这两步. 首先下载apache-ant-1.9.2-bin.tar.gz. 然后解压tar xvzf apache- ...

  10. PHP经典项目案例-(一)博客管理系统5

    本篇实现发表博客. 八.发表博客 (1).界面实现file.php <tr>      <td colSpan=3 valign="baseline" style ...