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. TFS WorkItem Permission Setting

    TFS非常强大,但是权限设置确实非常的恶心复杂,这貌似是一切NB又傲慢的软件的通病. 那么,在哪里设置 WorkItem 的权限呢? 第一步: 第二步: 第三步,下面你将一目了然. 第四步,Share ...

  2. oracle 表空间自己主动扩展大小

    select a.FILE_NAME,a.AUTOEXTENSIBLE,a.MAXBYTES,a.INCREMENT_BY  from  dba_data_files a; --AUTOEXTENSI ...

  3. C# WCF 完整实例,winform 窗体作为 宿主

    上一次提到,我们的WCF程序宿主是发布到IIS上面的.虽然这样做未尝不可,不过不便于我们进行“开始”或“停止”WCF服务的操作.所以再次尝试了编写以窗体应用程序作为WCF服务宿主的方式,并取得了成功. ...

  4. JQuery巧妙利用CSS操作打印样式

    一.添加打印样式 1. 为屏幕显示和打印分别准备一个css文件,如下所示:  用于屏幕显示的css: <link rel="stylesheet" href="cs ...

  5. 【用jQuery来判断浏览器的类型】及【javascript获取用户ip地址】

    用jQuery来判断浏览器的类型,主要是使用$.browser这个工具类,使用方法: $.browser.['浏览器关键字'] //谷歌浏览器.360浏览器等其他一些浏览器,没有专门的判断 funct ...

  6. c#递归生成XML

    递归方法大家应该都很熟悉了,简而言之就是方法内部调用自己,就这样不断重复重复再重复的执行, 不过要担心死循环哟... 当我们系统需要动态生成菜单时,也就是说我们系统的菜单是存在数据库中的,数据库结构类 ...

  7. Oracle根据字段值找到表名和列名

    方法1: --Oracle 根据字段值查询其所在的表.字段 DECLARE CURSOR cur_query IS SELECT table_name, column_name, data_type ...

  8. 如何将数据转换libsvm格式文件

    原文:http://blog.sina.com.cn/s/blog_5c2f929b0100qse8.html 有三种工具可用1.网上有一个xls文FormatDataLibsvm.xls具有宏命令, ...

  9. VS2008+Windows DDK 7的环境配置(二)

    在第一篇的基础上,进行如下的步骤,就可以编译出X64的驱动程序. (建议再另外建一个项目,这样避免混淆,因为x86和x64编译的有些编译选项是不同的.) 1. 安装VS2008 x64 build 组 ...

  10. (LeetCode 83)Remove Duplicates from Sorted Lists

    Given a sorted linked list, delete all duplicates such that each element appear only once. For examp ...