python solution


  • 0
    I
    class Solution(object):
        def deleteNode(self, root, key):
            arr=self.makeArrFromTreeWithoutKey(root,None,key)
            return self.makeBalancedTree(arr,0,len(arr)-1)
        def makeArrFromTreeWithoutKey(self, root, arr, key):
            if arr is None and root is None:
                return []
            if arr is None:
                arr=[]
            if root.left is None:
                if root.val != key:
                    arr.append(root.val)
            else:
                self.makeArrFromTreeWithoutKey(root.left, arr, key)
                if root.val != key:
                    arr.append(root.val)
            if root.right is not None:
                self.makeArrFromTreeWithoutKey(root.right, arr, key)
            return arr  
        def makeBalancedTree(self, arr, start, end):
            if start>end:
                return None
            mid = int(round((end+start)/2))
            node = TreeNode(arr[mid])
            node.left=self.makeBalancedTree(arr,start,mid-1)
            node.right=self.makeBalancedTree(arr,mid+1,end)
            return node
    

Log in to reply
 

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