【LeetCode OJ】Sum Root to Leaf Numbers

时间:2022-09-01 21:17:52
# 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. Linux 远程登录

    Linux一般作为服务器使用,而服务器一般放在机房,你不可能在机房操作你的Linux服务器. 这事我们就需要远程登录到Linux服务器来管理维护系统. Linux系统中是通过ssh服务实现的远程登录功 ...

  2. Sencha Touch 手机移动开发框架 HTML5 项目压缩方案&semi;

    Sencha Touch框架生成基本项目目录结构 Index.html/ App.js App.json /touch[sdk]/ /Sencha-touch.js /src Resources/ A ...

  3. hdu 猜数字

    这题的意思是找到最大的n使得m次之内的猜测可以猜到1~n之间的任何值.这里是二分思想的逆过程,1~h个数最多猜测log2(n+1)次(n为奇数),故 n=2^m-1; #include"io ...

  4. 【转】iOS-延迟操作方法总结

    原文网址:http://lysongzi.com/2016/01/30/iOS-%E5%BB%B6%E8%BF%9F%E6%93%8D%E4%BD%9C%E6%96%B9%E6%B3%95%E6%80 ...

  5. javascript正则

      <script type="text/javascript"> //去除两边空格,如果要去除所有空格,使用/\s*即可/ String.prototype.trim ...

  6. Node&period;js学习 - Buffer

    JavaScript 语言自身只有字符串数据类型,没有二进制数据类型.但在处理像TCP流或文件流时,必须使用到二进制数据. 因此在 Node.js中,定义了一个 Buffer 类,该类用来创建一个专门 ...

  7. kettle简介(整体架构,运行方式,使用方法)

    项目负责人Matt的说法:把各种数据放到一个壶里,然后呢,以一种你希望的格式流出.呵呵,外国人都很有联想力.看了提供的文档,然后对发布程序的简单试用后,可以很清楚得看到Kettle的四大块: Chef ...

  8. Android Gradle 依赖方式

    Android Gradle 依赖方式有以下6种: Compile compile是对所有的build type以及favlors都会参与编译并且打包到最终的apk文件中. Provided Prov ...

  9. MySQL crash-safe replication&lpar;2&rpar;&colon;

    MySQL数据库的成功离不开其replicaiton(复制),相对于Oracle DG和Microsoft SQL Server Log Shipping来说,其简单易上手,基本上1,2分钟内根据手册 ...

  10. flume 日志导入elasticsearch

    Flume配置 . flume生成的数据结构 <span style="font-size:18px;">"_index" : "logs ...