[LeetCode] 129. Sum Root to Leaf Numbers_Medium tag: DFS
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 path1->2
represents the number12
.
The root-to-leaf path1->3
represents the number13
.
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 path4->9->5
represents the number 495.
The root-to-leaf path4->9->1
represents the number 491.
The root-to-leaf path4->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的更多相关文章
- [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
原题地址 二叉树的遍历 代码: vector<int> path; int sumNumbers(TreeNode *root) { if (!root) ; ; path.push_ba ...
- [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 ...
- LeetCode 129. Sum Root to Leaf Numbers 动态演示
树的数值为[0, 9], 每一条从根到叶子的路径都构成一个整数,(根的数字为首位),求所有构成的所有整数的和 深度优先搜索,通过一个参数累加整数 class Solution { public: vo ...
- [LeetCode]129. Sum Root to Leaf Numbers路径数字求和
DFS的标准形式 用一个String记录路径,最后判断到叶子时加到结果上. int res = 0; public int sumNumbers(TreeNode root) { if (root== ...
- 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 ...
随机推荐
- 有重复行,查询时只保留最新一行的sql
一.表结构如下:表名test 二.sql select temp.* from (select test.*, row_number() over(partition by obd_code orde ...
- C# MVC+EF—结构搭建
近期做了MVC+EF的项目,现在项目完结了,抽个时间写个小DOM总结一下,顺便加深理解. 一.新建MVC项目,结构是这样的
- uploadify中文开发文档,解决php多图上传
图片上传好用插件有,比如 uploadify ueditor html5的各种ajax上传插件,大部分都是异步,返回只是true之类,有些时候需要上传图片需要一起上传,其实可以通过操作流程来避免这个 ...
- [No0000ED]IPSec策略之管理
IP安全策略 @echo off :again set num= set ippolicyname= set ismmpfs= set keytime= set keyexpress= set new ...
- ROADS POJ - 1724 约束最短路 暴搜 加剪枝
http://poj.org/problem?id=1724 题意:最短路的模板,不过每条边加上一个费用,要求总费用不超过k 题解:不能用dijkstra ,直接暴力,dfs维护len和cost. 普 ...
- CentOS7初始化mysql库报错
在centos7上安装mysql数据库,进行数据库初始化工作时,报错缺少data::dumper库文件,如下: 解决办法:安装autoconf库后重新初始化即可解决. yum-y install au ...
- MySQL获取分组后的TOP 1和TOP N记录-转
有时会碰到一些需求,查询分组后的最大值,最小值所在的整行记录或者分组后的top n行的记录,在一些别的数据库可能有窗口函数可以方面的查出来,但是MySQL没有这些函数,没有直接的方法可以查出来,可通过 ...
- AFNetworking的缓存使用
+ (NSURLCache *)defaultURLCache { // It's been discovered that a crash will occur on certain version ...
- iOS-方法之+ initialize 与 +load
Objective-C 有两个神奇的方法:+load 和 +initialize,这两个方法在类被使用时会自动调用.但是两个方法的不同点会导致应用层面上性能的显著差异. 一.+ initialize ...
- /etc/passwd- 和/etc/shadow-文件
今天偶尔看到系统里有/etc/passwd- 和/etc/shadow-文件,经测试只要执行过系统的用户操作命令就会产生,如deluser.passwd.chpasswd.adduser等命令,应该是 ...