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. 介绍Web项目中用到的几款表单验证插件

    第一个插件 jqueryvalidation 官网地址:http://jqueryvalidation.org/ 第二个插件 nice Validator 官网地址: http://niceue.co ...

  2. c#中的控件01

    1.常用控件: 只读文本:TextBlock.文本框:TextBox.按钮:Button 事件:鼠标移到按钮上的时候显示“大爷您来了”,离开 显示“大爷常来”,Click(点击).Loaded(控件加 ...

  3. React Native中加载指示器组件ActivityIndicator使用方法

    这里讲一下React Native中的一个组件——ActivityIndicator,这是一个加载指示器,俗称菊花,很常见的,效果如下所示: 可以看到图中有两个加载指示器,一大一小,这是尺寸不是我设置 ...

  4. redis入门笔记

    redis入门笔记 参考redis实战手册 1. Redis在windows下安装 下载地址:https://github.com/MSOpenTech/redis/tags 安装Redis 1.1. ...

  5. linux 基本命令___0003 字符串处理和yum安装软件的路径

    字符串变量的处理 参考链接:SHELL字符串处理技巧 计算字符串的字符数量: ${#str} str="xxx-Lane1_S2_L001_R1_trim.fastq" echo ...

  6. jsp——js事件修改属性样式的两种方法(直接赋值、修改属性)、验证表单符合某要求、阻止表单提交、告诉浏览器不要缓存

    代码 <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncodi ...

  7. 死循环的/etc/profile

    用户服务器登陆后停在以下界面 Connecting to ... Connection established. To escape to local shell, press 'Ctrl+Alt+] ...

  8. VUE 结合 vue-resource 进行ajax操作

    有意思的! 初始化需要ajax获取数据! 搜索商品需要ajax获取数据! 提交数据需要ajax传递数据! 有了 vue-resource ,操作挺方便的. 这是html <form class= ...

  9. JNI_Z_01_获取Clazz

    1. 为了能够在C/C++中使用Java类,jni.h头文件中专门定义了jclass类型来表示Java中的Class类(ZC: 就是Clazz) 2. 2.1.JNIEXPORT void JNICA ...

  10. DDOS 攻击工具

    DDOS  攻击工具 使用github上的DDOS攻击工具 https://github.com/Ha3MrX/DDos-Attack 将python脚本拷贝到主机,使用 chmod +x ddos- ...