【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 ...
随机推荐
- java编程思想
Java编程思想,Java学习必读经典,不管是初学者还是大牛都值得一读,这里总结书中的重点知识,这些知识不仅经常出现在各大知名公司的笔试面试过程中,而且在大型项目开发中也是常用的知识,既有简单的概念理 ...
- 整理的一些模版LCS(连续和非连续)
对于连续的最大串,我们称之为子串....非连续的称之为公共序列.. 代码: 非连续连续 int LCS(char a[],char b[],char sav[]){ int lena=strlen(a ...
- memcached 学习 1—— memcached+spring配置
memcached 学习目录: memcached 学习 1—— memcached+spring配置 这几天自己搭建项目环境,解决问题如下: 有关常见的配置这里没有列出,中间遇到的搭建问题比较顺利g ...
- HDU 4123(树的直径+单调队列)
Bob’s Race Time Limit: 5000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...
- Oracle存储过程写法
create or replace procedure QIANFEIGL_JIAOKUANDY( cebenh varchar2, kehuh varchar2, hetongh varchar2, ...
- 利用ODBC从SQLServer向Oracle中导数据
1.首先要在Oracle数据库中建对应的表,Oracle数据库中的字段类型和Sql Server 有所不同,Oracle中常用的有varchar2.integer.nchar.date,Sql Ser ...
- sql插入多条数据的sql语句
sql插入多条数据的sql语句 有三种方法:1.InSert Into <表名>(列名)Select <列名>From <源表名>如:INSERT INTO Ton ...
- Oracle连接的若干错误
用PL/SQL连接Oracle时会抛若干错误,如下: 1.ora-12154:TNS:无法解析指定的连接标识符 答:plsql在%Oracle_Home%\Network\Admin或者c:\inst ...
- Python中的random模块
Python中的random模块用于生成随机数.下面介绍一下random模块中最常用的几个函数. random.random random.random()用于生成一个0到1的随机符点数: 0 < ...
- 上次遗留下来的XMLUtil的问题
·在上周留下了一个关于XMLUtil的问题,问题大概是这样的,需要通过读取一个XML文件,然后在内存中生成一个对应的javaBean.之前写的那个很是糟糕,照着一个XML去写了一个"Util ...