Python straight forward O(n) not using global variable


  • 0
    V
    class Solution(object):
        def convertBST(self, root):
            """
            :type root: TreeNode
            :rtype: TreeNode
            """
            def rinorder(node,csum):
     
                if not node:
                    return 0
                cur=rinorder(node.right,csum)
                if not node.right:
                    node.val=node.val+csum
                else:
                    node.val=node.val+cur
                left=rinorder(node.left,node.val)
                if left==0:
                    return node.val
                else:
                    return left
            rinorder(root,0)
            return root
    

Log in to reply
 

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