Explain to me why this solution is bad


  • -1
    G

    I'm really working hard on improving my BST algorithms and understanding. It hasn't been easy but I've made some progress.

    The following is what I came up with before viewing the solutions, and I haven't really fully understood the proposed solutions yet.

    It'd be real helpful if someone could point out the shortcomings of this solution, as it does work.

        def convertBST(self, root):
            """
            :type root: TreeNode
            :rtype: TreeNode
            """
            if not root:
                return
            stack = [root]
            vals = []
            while stack:
                node = stack.pop(0)
                vals.append(node.val)
                if node.right:
                    stack.append(node.right)
                if node.left:
                    stack.append(node.left)
            
            def helper(node, vals):
                if not node:
                    return
                node.val += sum([num for num in vals if num > node.val])
                helper(node.left, vals)
                helper(node.right, vals)
                return node
            return helper(root, vals)
    
    
    Thank you

  • 0

    Why don't you tell us why it's bad? After all, you're the one calling it bad.


Log in to reply
 

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