题目如下:

Given a binary tree, each node has value 0 or 1.  Each root-to-leaf path represents a binary number starting with the most significant bit.  For example, if the path is 0 -> 1 -> 1 -> 0 -> 1, then this could represent 01101 in binary, which is 13.

For all leaves in the tree, consider the numbers represented by the path from the root to that leaf.

Return the sum of these numbers.

Example 1:

Input: [1,0,1,0,1,0,1]
Output: 22
Explanation: (100) + (101) + (110) + (111) = 4 + 5 + 6 + 7 = 22

Note:

  1. The number of nodes in the tree is between 1 and 1000.
  2. node.val is 0 or 1.
  3. The answer will not exceed 2^31 - 1.
 

解题思路:没啥好说的,递归遍历树吧。

代码如下:

# Definition for a binary tree node.
# class TreeNode(object):
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None class Solution(object):
res = 0
def recursive(self,node,path):
path += str(node.val)
if node.left == None and node.right == None:
self.res += int(path,2)
return
if node.left != None:
self.recursive(node.left,path)
if node.right != None:
self.recursive(node.right, path) def sumRootToLeaf(self, root):
"""
:type root: TreeNode
:rtype: int
"""
self.res = 0
self.recursive(root,'')
return self.res % (10 ** 9+ 7)

【leetcode】1022. Sum of Root To Leaf Binary Numbers的更多相关文章

  1. 【LeetCode】1022. Sum of Root To Leaf Binary Numbers 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 DFS 日期 题目地址:https://leetco ...

  2. 【Leetcode_easy】1022. Sum of Root To Leaf Binary Numbers

    problem 1022. Sum of Root To Leaf Binary Numbers 参考 1. Leetcode_easy_1022. Sum of Root To Leaf Binar ...

  3. Leetcode 1022. Sum of Root To Leaf Binary Numbers

    dfs class Solution: def sumRootToLeaf(self, root: TreeNode) -> int: stack=[(root,0)] ans=[] bi_st ...

  4. 1022. Sum of Root To Leaf Binary Numbers从根到叶的二进制数之和

    网址:https://leetcode.com/problems/sum-of-root-to-leaf-binary-numbers/ 递归调用求和,同时注意%1000000007的位置 /** * ...

  5. LeetCode 1022. 从根到叶的二进制数之和(Sum of Root To Leaf Binary Numbers)

    1022. 从根到叶的二进制数之和 1022. Sum of Root To Leaf Binary Numbers 题目描述 Given a binary tree, each node has v ...

  6. LeetCode.1022-根到叶路径二进制数之和(Sum of Root To Leaf Binary Numbers)

    这是小川的第381次更新,第410篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第243题(顺位题号是1022).给定二叉树,每个节点值为0或1.每个根到叶路径表示以最高 ...

  7. [Swift]LeetCode1022. 从根到叶的二进制数之和 | Sum of Root To Leaf Binary Numbers

    Given a binary tree, each node has value 0 or 1.  Each root-to-leaf path represents a binary number ...

  8. 【LeetCode】129. Sum Root to Leaf Numbers 解题报告(Python)

    [LeetCode]129. Sum Root to Leaf Numbers 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/pr ...

  9. 【LeetCode】863. All Nodes Distance K in Binary Tree 解题报告(Python)

    [LeetCode]863. All Nodes Distance K in Binary Tree 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http ...

随机推荐

  1. 分页框架pager-taglib学习笔记

    说到分页其实可以研究一下我自己项目里面的分页框架的使用. 下面的笔记来自于孔浩老师的视频教程和我自己的开发实践. 使用Pager-taglib可以帮助我们快速开发分页处理. 下载:pager-tagl ...

  2. Python3实现简单的钉钉机器人调用

    具体可以参考开发文档:https://ding-doc.dingtalk.com/doc#/serverapi3/iydd5h from urllib import parse, request im ...

  3. innobackupex对MySQL做热备份,报错mysql库下数据字典表损坏

    [root@node1 op]#mysql -Vmysql Ver 14.14 Distrib 5.6.29innobackupex热备份MySQL报错,报错信息:[root@node1 op]#in ...

  4. java中的本地缓存

    java中的本地缓存,工作后陆续用到,一直想写,一直无从下手,最近又涉及到这方面的问题了,梳理了一下.自己构造单例.guava.ehcache基本上涵盖了目前的大多数行为了.   为什么要有本地缓存? ...

  5. Gitlab仓库搭建和免密使用gitlab

    Gitlab简介 GitLab 是一个用于仓库管理系统的开源项目,使用Git作为代码管理工具,并在此基础上搭建起来的web服务. 可通过Web界面进行访问公开的或者私人项目.它拥有与Github类似的 ...

  6. 【ABAP系列】SAP ABAP基础-程序优化及响应速度之LOOP

    公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[ABAP系列]SAP ABAP基础-程序优化及 ...

  7. poj1258Agri-Net(最小生成树)

    题目链接:http://poj.org/problem?id=1258 Description Farmer John has been elected mayor of his town! One ...

  8. reuseaddr和点对点聊天

    解决绑定失败 在测试时,经常会出现绑定错误,bind error: Address already in use 这里只要指定一下socket的reuseaddr属性即可解决 int on=1; if ...

  9. oracle创建sequence序列语法

    在oracle中sequence就是序号,每次取的时候它会自动增加.sequence与表没有关系 1.create sequence create sequence SEQ_LOG_ID minval ...

  10. [Linux] 028 源码包安装过程

    1. 安装准备 安装 C 语言编译器 下载源码包 如:apache 相应源码包下载地址 2. 安装注意事项 源代码保存位置:/usr/local/src 软件安装位置:/usr/local 如何确定安 ...