# 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的更多相关文章

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

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

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

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

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

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

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

  8. 【树】Sum Root to Leaf Numbers

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

  9. LeetCode题解之Sum Root to Leaf Numbers

    1.题目描述 2.问题分析 记录所有路径上的值,然后转换为int求和. 3.代码 vector<string> s; int sumNumbers(TreeNode* root) { tr ...

随机推荐

  1. 从 bcp 客户端收到一个对 colid x 无效的列长度。

    出现场景: 批量插入数据的时候出现这个问题. 原因分析:某个数据的长度应该是大于这个数据对应的列的定义长度. 所以一一检查到底是那个列的长度超出了. 第一种方法: ——————————2017-1-3 ...

  2. 菜鸟学习Andriod-弹窗

    菜鸟学习Andriod-弹窗 return new AlertDialog.Builder(ZyScreenSaver.this).setIcon( R.drawable.ic_launcher).s ...

  3. JBoss JMX登录需要用户名密码的解决办法

    /opt/jboss/eap5.1.2/jboss-as/server/default/conf/props/jmx-console-users.properties 取消#admin=admin的注 ...

  4. mybatis使用

    mybatis网站:http://mybatis.github.io/spring/zh/ mybatis spring下载网址:https://github.com/mybatis/spring/r ...

  5. 使用VideoView播放视频

    为了在Android应用中播放视频,Android提供了VideoView组件,它就是一个位于android.widget包下的组件,它的作用与ImageView类似,只是ImageView用于显示图 ...

  6. 一天完成把PC网站改为自适应!原来这么简单!

    http://www.webkaka.com/blog/archives/how-to-modify-a-web-page-to-be-responsive.html 一天完成把PC网站改为自适应!原 ...

  7. web api同源策略

    1.重写JsonMediaTypeFormatter public class JsonpMediaTypeFormatter : JsonMediaTypeFormatter { private s ...

  8. IBatis.Net系列-多参数的SQL语句的配置

    我们在使用IBatis.net操作数据的时候,肯定会碰到SQL参数当我们有一个参数时,IBatis的xml映射文件如下: <statement id="getProduct" ...

  9. mac下安装apache+php+mysql

    运行“sudo apachectl start”,再输入帐号密码,这样Apache就运行了. 运行“sudo apachectl -v”,你会看到Mac OS X 10.6.3中的Apache版本号: ...

  10. WP8.1 Study5:Data binding数据绑定

    一.数据绑定 最简单的编程UI控件的方法是写自己的数据来获取和设置控件的属性,e.g. , textBox1.Text = "Hello, world"; 但在复杂的应用程序,这样 ...