반응형
leetcode.com/problems/binary-search-tree-to-greater-sum-tree/
# 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 = right
class Solution:
sum =0
def bstToGst(self, root: TreeNode) -> TreeNode:
def recur(node):
if not node:
return
node.right = recur(node.right)
self.sum += node.val
node.val = self.sum
node.left = recur(node.left)
return node
return recur(root)
댓글