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

做者: 负雪明烛
id: fuxuemingzhu
我的博客: http://fuxuemingzhu.cn/node


题目地址:https://leetcode.com/problems/sum-of-root-to-leaf-binary-numbers/python

题目描述

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

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 modulo 10^9 + 7.this

Example 1:spa

此处输入图片的描述

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

Note:code

  1. The number of nodes in the tree is between 1 and 1000.
  2. node.val is 0 or 1.

题目大意

二叉树的每一个节点的数值有0和1,从根节点到叶子节点的一条路径就会构成了一个二进制字符串,求全部路径构成的二进制字符串转成整数的和。blog

解题方法

DFS

最简单的方法就是把全部的路径遍历一遍,而后计算每条路径构成的二进制字符串表示的整数的最大值便可。递归

我认为递归最重要的是明白递归函数的定义,若是不能明白定义,那么就写不出正确的代码。图片

这个题定义了一个函数,查找的是在遍历这个节点之时,已经路过的路径(包括当前节点)。在其中判断若是该节点是叶子节点,那么更新全部的路径和。

Python的整数不会越界,这题中每经历过一个节点,就把以前的路径*2 + 当前的节点值当作路径表示的整数。

Python代码以下:

# 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):
    def sumRootToLeaf(self, root):
        """ :type root: TreeNode :rtype: int """
        if not root: return 0
        self.res = 0
        self.dfs(root, root.val)
        return self.res

    def dfs(self, root, preSum):
        if not root.left and not root.right:
            self.res = (self.res + preSum) % (10 ** 9 + 7)
            return
        if root.left:
            self.dfs(root.left, preSum * 2 + root.left.val)
        if root.right:
            self.dfs(root.right, preSum * 2 + root.right.val)

日期

2019 年 4 月 7 日 —— 周赛bug了3次。。

相关文章
相关标签/搜索