0
点赞
收藏
分享

微信扫一扫

LC129. 求根节点到叶节点数字之和

婉殇成长笔记 2022-04-06 阅读 40
python

先序遍历就行

def sumNumbers(self, root):
        """
        :type root: TreeNode
        :rtype: int
        """
        def pre(root,pretotal):
            if not root:
                return 0
            total = pretotal*10 + root.val
            if not root.left and not root.right:
                return total
            return pre(root.left,total) + pre(root.right,total)
        return pre(root,0)
举报

相关推荐

0 条评论