Simple Recursive Python Solution. (115 ms)


  • 0
    A
    # Definition for a binary tree node.
    # class TreeNode(object):
    #     def __init__(self, x):
    #         self.val = x
    #         self.left = None
    #         self.right = None
    
    class Solution(object):
        def add(self , node ,ls):
            if(node == None):
                return 0
            else:
                rightsum = self.add(node.right,ls)
                leftsum = self.add(node.left,ls+node.val+rightsum)
                curr = node.val
                node.val += (rightsum+ls)
                return leftsum+rightsum+curr
        def convertBST(self, root):
            self.add(root,0)
            return root
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.