DFS

LeetCode 1022 Sum of Root To Leaf Binary Numbers (Python)

Posted by 小明MaxMing on September 8, 2020

题目

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.

解题思路

使用DFS遍历整棵树,遍历时记录从根到当前结点所构成的数,如果是叶节点,则加到结果里

代码

class Solution:
    def sumRootToLeaf(self, root: TreeNode) -> int:
        res = 0
        stack = [(root, 0)]
        while stack:
            root, num = stack.pop()
            if root:
                num = (num << 1) | root.val
                if not root.left and not root.right:
                    res += num
                else:
                    stack.append((root.right, num))
                    stack.append((root.left, num))
        return res

视频讲解 YouTube<--欢迎点击订阅

视频讲解 bilibili<--欢迎点击订阅