Python Iterative inorder in 95ms (beat 99.67% so far)


  • 0
    L

    I'm just not a big fan of recursion.....

        def convertBST(self, root):
            """
            :type root: TreeNode
            :rtype: TreeNode
            """
            if root == None:
                return None
            
            nodeSum = 0
            done = False
            current = root
            stack = []
            while not done:
                while current:
                    stack.append(current)
                    current = current.right
                if stack:
                    current = stack.pop()
                    nodeSum = nodeSum + current.val
                    current.val = nodeSum
                    current = current.left
                else:
                    done = True
            return root
    

Log in to reply
 

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