【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 ...
随机推荐
- jQuery/CSS3 图片边框线条变换动画
在线演示 本地下载
- Java学习之垃圾回收
垃圾回收(GC) GC需要完成的三件事情: 哪些内存需要回收? 什么时候回收? 如何回收? 为什么"GC自动化"之后还要研究GC?当需要排查各种内存溢出.内存泄漏问题时,当GC成为 ...
- 如何理解 Python 中的__init__
转自https://www.zhihu.com/question/46973549/answer/103805810 定义类的时候,若是添加__init__方法,那么在创建类的实例的时候,实例会自动调 ...
- 行列转换文本处理--awk xargs 回顾
awk 数组回顾: 9.1 数组 举例:统计当前主机上每一个TCP连接状态以及每种连接状态的数目[非常实用] # netstat -tan | awk '/^tcp/{STATE[$NF]++}END ...
- Python 类的三大特性的综合运用 案例
# --------------------- 类的三大特性的综合运用 案例 ------------------------- # 定义三个类:小狗,小猫,人 # 小狗:姓名,年龄(默认1岁) 吃饭 ...
- protobuf与json转换
protobuf对象不能直接使用jsonlib去转,因为protobuf生成的对象的get方法返回的类型有byte[],而只有String类型可以作为json的key,protobuf提供方法进行转换 ...
- Spark- JdbcRDD以及注意事项
先上Demo package com.rz.spark.base import java.sql.DriverManager import org.apache.spark.rdd.JdbcRDD i ...
- Minhash 算法 及其应用
背景: 我遇到一个问题,要计算140万商品的杰卡德相似度.如果直接要直接两两计算的话,这计算量根本算不了,而且也没必要. 分析: 在这些商品中很多商品的相似度并不高,也就是说其中达到相似度阈值的商品只 ...
- 英语每日阅读---3、VOA慢速英语(翻译+字幕+讲解):哈佛大学被控歧视亚裔学生
英语每日阅读---3.VOA慢速英语(翻译+字幕+讲解):哈佛大学被控歧视亚裔学生 一.总结 一句话总结:Harvard Accused of Discriminating Against Asian ...
- 使用Java代码来创建view
使用Java代码来创建view 一.简介 需要了解的知识 二.方法 1)java代码创建view方法 * 1.先建view对象 View view= View.inflate(this, R.layo ...