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. 1
  2. / \
  3. 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

  1. class Solution {
  2. public:
  3. int sumNumbers(TreeNode* root) {
  4. if (!root)
  5. return ;
  6. return dfs(root, );
  7. }
  8. int dfs(TreeNode* root, int sum) {
  9. if (!root)
  10. return ;
  11. sum = sum * + root->val;
  12. if (!root->left && !root->right)
  13. return sum;
  14. return dfs(root->left, sum) + dfs(root->right, sum);
  15. }
  16. };

Solution 2

  1. class Solution {
  2. public:
  3. int sumNumbers(TreeNode* root) {
  4. if (!root)
  5. return ;
  6. queue<TreeNode*> q1;
  7. queue<int> q2;
  8. q1.push(root);
  9. q2.push(root->val);
  10.  
  11. int sum = , cursum = ;
  12. TreeNode* cur = root;
  13.  
  14. while (!q1.empty()) {
  15. cur = q1.front();
  16. cursum = q2.front();
  17. q1.pop();
  18. q2.pop();
  19.  
  20. if (!cur->left && !cur->right) {
  21. sum += cursum;
  22. }
  23. if (cur->left) {
  24. q1.push(cur->left);
  25. q2.push(cursum * + cur->left->val);
  26. }
  27. if (cur->right) {
  28. q1.push(cur->right);
  29. q2.push(cursum * + cur->right->val);
  30. }
  31. }
  32. return sum;
  33. }
  34. };

Solution 3

  1. class Solution {
  2. public:
  3. int sumNumbers(TreeNode* root) {
  4. if (!root)
  5. return ;
  6. stack<TreeNode*> s1;
  7. stack<int> s2;
  8. int sum = , cursum = ;
  9. TreeNode* cur = root, *pre = nullptr;
  10. while (cur || !s1.empty()) {
  11. while (cur) {
  12. s1.push(cur);
  13. cursum = cursum * + cur->val;
  14. s2.push(cursum);
  15. cur = cur->left;
  16. }
  17. cur = s1.top();
  18. if (!cur->left && !cur->right) {
  19. sum += cursum;
  20. }
  21. if (cur->right && cur->right != pre) {
  22. cur = cur->right;
  23. } else {
  24. pre = cur;
  25. s1.pop();
  26. cursum = s2.top();
  27. s2.pop();
  28. cursum -= cur->val;
  29. cursum /= ;
  30. cur = nullptr;
  31. }
  32. }
  33. return sum;
  34. }
  35. };

【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. 20145219 《Java程序设计》第02周学习总结

    20145219 <Java程序设计>第02周学习总结 教材学习内容总结 类型:基本类型.类类型(参考类型) 基本类型: 整数:short占2字节,int占4字节,long占8字节 字节: ...

  2. SpringBoot 定义通过字段验证

    第一步:定义ValidationResult类 public class ValidationResult { // 校验结果是否有错 private boolean hasErrors = fals ...

  3. 最大字串和问题(Maximum Subarray)

    问题描述: ind the contiguous subarray within an array (containing at least one number) which has the lar ...

  4. Nessus离线安装及升级插件 转

    修改Nessus Web端口 ./nessuscli fix --set xmlrpc_listen_port=8866 最近做客户的内网主机漏洞扫描,申请了一台内网主机做扫描服务器,安装Nessus ...

  5. NLP(三)_统计语言模型

    概念 统计语言模型:是描述自然语言内在的规律的数学模型.广泛应用于各种自然语言处理问题,如语音识别.机器翻译.分词.词性标注,等等.简单地说,语言模型就是用来计算一个句子的概率的模型 即P(W1,W2 ...

  6. python标准日志模块logging使用

    python的标准库里的日志系统从Python2.3开始支持.只要import logging这个模块即可使用.如果你想开发一个日志系统, 既要把日志输出到控制台, 还要写入日志文件,只要这样使用: ...

  7. 将本地代码提交到gitlub

    第一步:建立git仓库  cd到本地项目根路径下面,执行git命令:git  init $ git init Initialized empty Git repository in D:/my_wor ...

  8. 使用swagger作为restful api的doc文档生成——从源码中去提取restful URL接口描述文档

    初衷 记得以前写接口,写完后会整理一份API接口文档,而文档的格式如果没有具体要求的话,最终展示的文档则完全决定于开发者的心情.也许多点,也许少点.甚至,接口总是需要适应新需求的,修改了,增加了,这份 ...

  9. 条款28:避免返回handles指向对象的内部成分。

    首先看看下面这个例子: class Point{ public: point(int x, int y); ... void setX(int newVal); void setY(int newVa ...

  10. int 21h 汇编

    INT 21H 指令说明及使用方法 转自http://www.cnblogs.com/ynwlgh/archive/2011/12/12/2285017.html 很多初学汇编语言的同学可能会对INT ...