【LeetCode OJ】Sum Root to Leaf Numbers
# Definition for a binary tree node
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None class Solution:
# @param root, a tree node
# @return an integer
def sumNumbers(self, root):
"""
BFS the tree from the root
track each path as a root->leaf number
"""
# specail cases
if root is None:
return 0 # queue of the numbers
# BSF the tree
q = [ (root, 0) ]
res = 0
while q:
new_q = []
# expand all paths in q by one step
for (node, number) in q:
node_number = number * 10 + node.val
# If the node is a leaf, remove this path and record the root-leaf number
if node.left == node.right == None:
res += node_number
# Expand the path by left/right children
else:
if node.left:
new_q.append( (node.left, node_number) )
if node.right:
new_q.append( (node.right, node_number) )
# Update the path list
q = new_q return res
Problem Link:
http://oj.leetcode.com/problems/sum-root-to-leaf-numbers/
This problem is easy to solve by using BFS from the tree root.
We use a list to keep track of different paths, where each path is represented as a number.
【LeetCode OJ】Sum Root to Leaf Numbers的更多相关文章
- 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 OJ: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解题报告—— 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】Sum Root to Leaf Numbers(hard)
Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a number ...
- 【LeetCode】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】【Medium】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刷题笔记】Sum Root to Leaf Numbers
Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a number ...
- 【树】Sum Root to Leaf Numbers
题目: Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a nu ...
- LeetCode题解之Sum Root to Leaf Numbers
1.题目描述 2.问题分析 记录所有路径上的值,然后转换为int求和. 3.代码 vector<string> s; int sumNumbers(TreeNode* root) { tr ...
随机推荐
- 笔记2:傻瓜式盗QQ程序
1.一个简单的盗QQ号的方法 仿做一个QQ的窗体 PS:当然里面有用的控件只有两个输入框和一个登陆按钮,其他的尽量做得像一些吧! 点登陆时的后台代码: PS:需要导入两个包:using System. ...
- addViewController之后view里面的点击事件不响应
let dealsSeeMoreViewController = DealsSeeMoreViewController(owner: self) self.dealsStackView.addArra ...
- hadoop入门之设置datanode的心跳时间的方法
做作业的过程中发现,把一节点停掉,dfsadmin和50070都无法马上感知到一个data node已经死掉 HDFS默认的超时时间为10分钟+30秒.这里暂且定义超时时间为timeout计算公式为: ...
- placeholder修改颜色
:-moz-placeholder { /* Mozilla Firefox 4 to 18 */ color: #f00; } ::-moz-placeholder { /* Mozilla Fir ...
- Hadoop MapReduceV2(Yarn) 框架简介[转]
对于业界的大数据存储及分布式处理系统来说,Hadoop 是耳熟能详的卓越开源分布式文件存储及处理框架,对于 Hadoop 框架的介绍在此不再累述,读者可参考 Hadoop 官方简介.使用和学习过老 H ...
- 二分图 最大权匹配 km算法
这个算法的本质还是不断的找增广路: KM算法的正确性基于以下定理:若由二分图中所有满足A[i]+B[j]=w[i,j]的边(i,j)构成的子图(称做相等子图)有完备匹配,那么这个完备匹配就是二分图的最 ...
- Redis persistence demystified - part 2
重写AOF 当AOF文件太大时,Redis将在临时文件重新写入新的内容.重写不会读取旧的AOF文件,而是直接访问内存中数据,以便让新产生的AOF文件最小,重写过程不需要读取磁盘. 重写完成后,Redi ...
- 如何在 Linux 中整理磁盘碎片
有一个神话是 linux 的磁盘从来不需要整理碎片.在大多数情况下这是真的,大多数因为是使用的是优秀的日志文件系统(ext3.4等等)来处理文件系统.然而,在一些特殊情况下,碎片仍旧会产生.如果正巧发 ...
- UVa 11426 - GCD - Extreme (II)
http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&p ...
- SAP iDoc 概念及管理
创建IDOC: 第一步:WE31 创建IDOC所包含的字段. 第二步:WE30 创建IDOC 把Segment分配给IDOC 第三步:WE81 创建信息类型 第四步:WE82 把 ...