1038. Binary Search Tree to Greater Sum Tree
Medium
Input: root = [4,1,6,0,2,5,7,null,null,null,3,null,null,null,8]
Output: [30,36,21,36,35,26,15,null,null,null,33,null,null,null,8]Input: root = [0,null,1]
Output: [1,null,1]Last updated
Input: root = [4,1,6,0,2,5,7,null,null,null,3,null,null,null,8]
Output: [30,36,21,36,35,26,15,null,null,null,33,null,null,null,8]Input: root = [0,null,1]
Output: [1,null,1]Last updated
# 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:
def __init__(self):
self.total = 0
def bstToGst(self, root: TreeNode) -> TreeNode:
if root is not None:
self.bstToGst(root.right)
self.total += root.val
root.val = self.total
self.bstToGst(root.left)
return root