Binary Search Tree to Greater Sum Tree (Leetcode 1038)

Problem Link: https://leetcode.com/problems/binary-search-tree-to-greater-sum-tree/

class Solution:
    def bstToGst(self, root: TreeNode) -> TreeNode:
        
        self.summ=0
        def fun(root):
            global summ
            if(root is None):
                return
            
            fun(root.right)
            self.summ=self.summ+root.val
            root.val=summ 
            fun(root.left)
            
        fun(root)
        return root

Last updated