【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 ...
随机推荐
- EasyUI:datagrid数据汇总
EasyUI:datagrid数据汇总 js代码: var total=0;//全局变量 $(function(){ $('#tablebudgetdata').datagrid({ title:' ...
- HTML5抽奖转盘
在线演示 本地下载
- (+4)2.2UML建模图
①用例图 [用途]:帮助开发团队以一种可视化的方式理解系统的功能需求. 用例图所包含的元素如下: 1. 参与者(Actor) 表示与您的应用程序或系统进行交互的用户.组织或外部系统.用一个小人表示. ...
- install tabix/bgzip
bgzip – Block compression/decompression utility tabix – Generic indexer for TAB-delimited genome pos ...
- hibernate的一对多配置
首先是“一”的 Customer.java package com.xiaostudy.domain; import java.util.HashSet; import java.util.Set; ...
- 理解Java中字符流与字节流的区别(转)
1. 什么是流 Java中的流是对字节序列的抽象,我们可以想象有一个水管,只不过现在流动在水管中的不再是水,而是字节序列.和水流一样,Java中的流也具有一个“流动的方向”,通常可以从中读入一个字节序 ...
- ctci1.1
) ; ; i < len; i++){ if(place.find(str[i]) == place.end()) place. ...
- 关于Action和EventHandler
.net框架自带的两个常用类(Action和EventHandler),当然这两个类型的也可以自定义,但系统已经提供,直接拿来用即可,很方便 1:Action : 引用“void方法”的委托,目前框架 ...
- sizeof结构体
规则1:结构体的对折长度为其基本数据成员的长度的最大值. 规则2:指定边界情况下,结构体的对折长度为自身对折长度和指定对折长度中较小者. 规则3:当行内结构体的基本数据成员的起始地址必须为其长度的整数 ...
- telnet: connect to address ::1: connect refused [centos, linux]
在我测试邮件服务器时, 使用 telnet localhost 25 出现如下的提示: Trying::1... telnet: connect to address ::1:Connection r ...