Python simple solution in a loop, 10 lines, no sub function, no recursive


  • 0
    Z
        def convertBST(self, root):
            """
            :type root: TreeNode
            :rtype: TreeNode
            """
            p, stack, sum_ = root, [], 0
            while p or stack:
                if p:
                    stack.append(p)
                    p = p.right
                else:
                    p = stack.pop()
                    p.val = sum_ = p.val + sum_
                    p = p.left
            return root
    

Log in to reply
 

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