【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
.
题解:
类似于 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的更多相关文章
- 【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 (2 solutions)
Sum Root to Leaf Numbers Given a binary tree containing digits from 0-9 only, each root-to-leaf path ...
- 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 ...
随机推荐
- 20145219 《Java程序设计》第02周学习总结
20145219 <Java程序设计>第02周学习总结 教材学习内容总结 类型:基本类型.类类型(参考类型) 基本类型: 整数:short占2字节,int占4字节,long占8字节 字节: ...
- SpringBoot 定义通过字段验证
第一步:定义ValidationResult类 public class ValidationResult { // 校验结果是否有错 private boolean hasErrors = fals ...
- 最大字串和问题(Maximum Subarray)
问题描述: ind the contiguous subarray within an array (containing at least one number) which has the lar ...
- Nessus离线安装及升级插件 转
修改Nessus Web端口 ./nessuscli fix --set xmlrpc_listen_port=8866 最近做客户的内网主机漏洞扫描,申请了一台内网主机做扫描服务器,安装Nessus ...
- NLP(三)_统计语言模型
概念 统计语言模型:是描述自然语言内在的规律的数学模型.广泛应用于各种自然语言处理问题,如语音识别.机器翻译.分词.词性标注,等等.简单地说,语言模型就是用来计算一个句子的概率的模型 即P(W1,W2 ...
- python标准日志模块logging使用
python的标准库里的日志系统从Python2.3开始支持.只要import logging这个模块即可使用.如果你想开发一个日志系统, 既要把日志输出到控制台, 还要写入日志文件,只要这样使用: ...
- 将本地代码提交到gitlub
第一步:建立git仓库 cd到本地项目根路径下面,执行git命令:git init $ git init Initialized empty Git repository in D:/my_wor ...
- 使用swagger作为restful api的doc文档生成——从源码中去提取restful URL接口描述文档
初衷 记得以前写接口,写完后会整理一份API接口文档,而文档的格式如果没有具体要求的话,最终展示的文档则完全决定于开发者的心情.也许多点,也许少点.甚至,接口总是需要适应新需求的,修改了,增加了,这份 ...
- 条款28:避免返回handles指向对象的内部成分。
首先看看下面这个例子: class Point{ public: point(int x, int y); ... void setX(int newVal); void setY(int newVa ...
- int 21h 汇编
INT 21H 指令说明及使用方法 转自http://www.cnblogs.com/ynwlgh/archive/2011/12/12/2285017.html 很多初学汇编语言的同学可能会对INT ...