Skip to main content

🟡 剑指 Offer II 054. 所有大于等于节点的值之和

LeetCode 提示

题目难度 中等

原题链接 🔗 leetcode

题解 1.py#

# Definition for a binary tree node.# class TreeNode:#     def __init__(self, val=0, left=None, right=None):#         self.val = val#         self.left = left#         self.right = rightclass Solution:    def convertBST(self, root: TreeNode) -> TreeNode:        if not root:            return root        self.curSum = 0
        def rmlVisit(node: TreeNode):            if node.right:                rmlVisit(node.right)            self.curSum += node.val            node.val = self.curSum            if node.left:                rmlVisit(node.left)                rmlVisit(root)        return root