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.

Note: A leaf is a node with no children.

Example:

Input: [1,2,3]
1
/ \
2 3
Output: 25
Explanation:
The root-to-leaf path 1->2 represents the number 12.
The root-to-leaf path 1->3 represents the number 13.
Therefore, sum = 12 + 13 = 25.

Example 2:

Input: [4,9,0,5,1]
4
/ \
9 0
 / \
5 1
Output: 1026
Explanation:
The root-to-leaf path 4->9->5 represents the number 495.
The root-to-leaf path 4->9->1 represents the number 491.
The root-to-leaf path 4->0 represents the number 40.
Therefore, sum = 495 + 491 + 40 = 1026. 思路就是DFS, 然后将node 和当前的path组成的数字string一起append进入stack, 判断如果是leaf, 将num转换为int加入在ans中. 1. Constraints
1)None => 0 2. Ideas
DFS T: O(n) S; O(n) 3. Code
class Solution:
def sumRootLeaf(self, root):
if not root: return 0
stack, ans = [(root, str(root.val))], 0
while stack:
node, num = stack.pop()
if not node.right and not node.left:
ans += int(num)
if node.left:
stack.append((node.left, num + str(node.left.val)))
if node.right:
stack.append((node.right, num + str(node.right.val)))
return ans

[LeetCode] 129. Sum Root to Leaf Numbers_Medium tag: DFS的更多相关文章

  1. [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 ...

  2. 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 ...

  3. 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 ...

  4. Leetcode#129 Sum Root to Leaf Numbers

    原题地址 二叉树的遍历 代码: vector<int> path; int sumNumbers(TreeNode *root) { if (!root) ; ; path.push_ba ...

  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. 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 ...

  7. LeetCode 129. Sum Root to Leaf Numbers 动态演示

    树的数值为[0, 9], 每一条从根到叶子的路径都构成一个整数,(根的数字为首位),求所有构成的所有整数的和 深度优先搜索,通过一个参数累加整数 class Solution { public: vo ...

  8. [LeetCode]129. Sum Root to Leaf Numbers路径数字求和

    DFS的标准形式 用一个String记录路径,最后判断到叶子时加到结果上. int res = 0; public int sumNumbers(TreeNode root) { if (root== ...

  9. 129. Sum Root to Leaf Numbers(Tree; DFS)

    Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a number ...

随机推荐

  1. Oracle数据库版本10.2.0.1升级到10.2.0.3(转)

    Oracle数据库版本10.2.0.1升级到10.2.0.3 1.停止OEM/isqlplus/监听/DB实例 $ emctl stop dbconsole $ isqlplusctl stop $ ...

  2. 使用介质设备安装 AIX 以通过 HMC 安装分区

    使用介质设备安装 AIX 以通过 HMC 安装分区 原文:https://www.ibm.com/support/knowledgecenter/zh/ssw_aix_72/com.ibm.aix.h ...

  3. Maven知识点积累二

    ①maven常用命令: mvn clean:清除target下编译生成的class文件 mvn compile:编译 mvn package:打包放到target下 mvn install:打包并放到 ...

  4. 开发人员如何从官网首页进入下载JDK历史版本

    就是下面的这篇文章,好心好意提交到百度经验,希望给需要的人一个帮助,结果被拒,说有广告.呵呵,oracle和java真的需要在你百度上面做广告吗?倒是能理解,可能是外行人做的,只是看到链接就拒了,但是 ...

  5. [No0000155]为什么32位机器最大只能用到4GB内存

    在此之前先来了解一些计算机存储单位之间的关系以及计算机系统结构和PC硬件方面的一些知识. 一.计算机存储单位之间的关系 ,最小的存储单位. 个二进制位为一个字节(B),即1B = 8bit,最常用的单 ...

  6. CountDownLatch简单使用

    CountDownLatch介绍 CountDownLatch是JAVA提供在java.util.concurrent包下的一个辅助类,可以把它看成是一个计数器,其内部维护着一个count计数,只不过 ...

  7. hive优化之调整mapreduce数目

    一.调整hive作业中的map数 1.通常情况下,作业会通过input的目录产生一个或者多个map任务.主要的决定因素有: input的文件总个数,input的文件大小,集群设置的文件块大小(目前为1 ...

  8. 20165225《Java程序设计》第五周学习总结

    20165225<Java程序设计>第五周学习总结 1.视频与课本中的学习: - 第七章学习总结 内部类: 内部类的外嵌类的成员变量在内部类中仍然有效,内部类中的方法也可以调用外嵌类中的方 ...

  9. Maven之基本概念及特性的基本介绍

    maven最主要的概念是坐标和依赖,这是maven可以极大简化构建过程以及进行项目管理的基础. 坐标 类似于地理位置的坐标,maven的坐标也是用来标记的,不同是它是来标记maven中的不同组件,也就 ...

  10. 使用反向代理的http的请求流程

    此文章主要为刚接触反向代理的小伙伴梳理请求流程,以便更好的理解反向代理是何时工作的 流程 由于浏览器是有缓存的,所以本地的hosts文件的信息也会在浏览器端缓存 当客户端发起一个新的请求(例如:输入的 ...