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.

题解:

  类似于 Path Sum 和  Path Sum II,迭代方法依然是后序遍历的思路,先遍历左右孩子。

Solution 1

 class Solution {
public:
int sumNumbers(TreeNode* root) {
if (!root)
return ;
return dfs(root, );
}
int dfs(TreeNode* root, int sum) {
if (!root)
return ;
sum = sum * + root->val;
if (!root->left && !root->right)
return sum;
return dfs(root->left, sum) + dfs(root->right, sum);
}
};

Solution 2

 class Solution {
public:
int sumNumbers(TreeNode* root) {
if (!root)
return ;
queue<TreeNode*> q1;
queue<int> q2;
q1.push(root);
q2.push(root->val); int sum = , cursum = ;
TreeNode* cur = root; while (!q1.empty()) {
cur = q1.front();
cursum = q2.front();
q1.pop();
q2.pop(); if (!cur->left && !cur->right) {
sum += cursum;
}
if (cur->left) {
q1.push(cur->left);
q2.push(cursum * + cur->left->val);
}
if (cur->right) {
q1.push(cur->right);
q2.push(cursum * + cur->right->val);
}
}
return sum;
}
};

Solution 3

 class Solution {
public:
int sumNumbers(TreeNode* root) {
if (!root)
return ;
stack<TreeNode*> s1;
stack<int> s2;
int sum = , cursum = ;
TreeNode* cur = root, *pre = nullptr;
while (cur || !s1.empty()) {
while (cur) {
s1.push(cur);
cursum = cursum * + cur->val;
s2.push(cursum);
cur = cur->left;
}
cur = s1.top();
if (!cur->left && !cur->right) {
sum += cursum;
}
if (cur->right && cur->right != pre) {
cur = cur->right;
} else {
pre = cur;
s1.pop();
cursum = s2.top();
s2.pop();
cursum -= cur->val;
cursum /= ;
cur = nullptr;
}
}
return sum;
}
};

【LeetCode】129. Sum Root to Leaf Numbers的更多相关文章

  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 (2 solutions)

    Sum Root to Leaf Numbers Given a binary tree containing digits from 0-9 only, each root-to-leaf path ...

  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内存模型

    多线程缓存一致性问题 程序在运行过程中,会将运算需要的数据从主存复制一份到CPU的高速缓存当中,那么CPU进行计算时就可以直接从它的高速缓存读取数据和向其中写入数据,当运算结束之后,再将高速缓存中的数 ...

  2. 0927-转载:SSM:spring+springmvc+mybatis框架中的XML配置文件功能详细解释

    这篇文章暂时只对框架中所要用到的配置文件进行解释说明,而且是针对注解形式的,框架运转的具体流程过两天再进行总结. spring+springmvc+mybatis框架中用到了三个XML配置文件:web ...

  3. JavaWeb 自定义标签库开发传统标签

    自定义标签主要用于移除Jsp页面中的java代码. 移除jsp页面中的java代码,只需要完成两个步骤: 编写一个实现Tag接口的Java类,并覆盖doStartTag方法,把jsp页面中的java代 ...

  4. Jenkins自动打包配置

    当时也是花费了不少时间来配置Jenkins自动打包的问题,觉得还是需要记录一下. 1.安装Jenkins,这个很简单,不需要多说. 2.下载Git Plugin,Gradle Plugin,Andro ...

  5. ELK分布式日志收集搭建和使用

    大型系统分布式日志采集系统ELK全框架 SpringBootSecurity1.传统系统日志收集的问题2.Logstash操作工作原理3.分布式日志收集ELK原理4.Elasticsearch+Log ...

  6. Makefile文件应用——huge项目

    提高复用性 在build目录下,保存公用部分make.rule (1)绝对路径 用ROOT变量保存项目根目录 (2)增加控制变量 EXE/LIB/ (3)头文件查找目录 gcc 的-I(i的大写)选项 ...

  7. Dev控件-gridview的属性说明

    说明 Options OptionsBehavior 视图的行为选项 AllowIncrementalSearch 允许用户通过输入想得到的列值来定位行 AllowPartialRedrawOnScr ...

  8. dataframe 列名重新排序

    在用list包含多个dict的模式生成dataframe时,由于dict的无序性,而uci很多数据的特征名直接是1,2,3...,生成的dataframe和原生的不一样, 为了方便观看和使用,我们将其 ...

  9. scala学习手记35 - 隐式类型转换

    先来看一下下面的内容: 2 days "ago" 5 days "from_now" 如上的内容具体应该是什么呢?不过怎么看也不像是代码.不过既然是在学代码,拿 ...

  10. 带你彻底明白 Android Studio 打包混淆

    前言 在使用Android Studio混淆打包时,该IDE自身集成了Java语言的ProGuard作为压缩,优化和混淆工具,配合Gradle构建工具使用很简单.只需要在工程应用目录的gradle文件 ...