Inconsistent solution while submitting


  • 0
    A

    For the problem Convert BST to Greater Tree, I wrote the code as below:

    class Solution(object):
        node_list=[]
        def convertBST(self, root):
            """
            :type root: TreeNode
            :rtype: TreeNode
            """
            node_dict=defaultdict(int)
            self.inorder(root)
            #print 'List of nodes is: ',self.node_list
            
            first = self.node_list[0]
            node_dict[first] = first
            
            temp = self.node_list[::-1]
            previous=0
            for ind,elem in enumerate(temp):
                #print 'Element is: ',elem
                node_dict[elem] = previous+elem
                previous=node_dict[elem]
                #print 'Its sum is:', node_dict[elem]
                
            root = self.replace(root,node_dict)
            return root
            
        def inorder(self,root):
            if root is None:
                return
            self.inorder(root.left)
            self.node_list.append(root.val)
            self.inorder(root.right)
            
        def replace(self,root,node_dict):
            if root is None:
                return
            self.replace(root.left,node_dict)
            key=root.val
            root.val = node_dict[key]
            self.replace(root.right,node_dict)
            return root
    

    The problem is: on running the solution locally, I am getting the correct result but on Submitting, I am getting incorrect output for the same input!

    Please see the attached snapshot for the same!
    Could anyone please point out the error here?0_1500662374657_Screen Shot 2017-07-21 at 11.37.38.png


Log in to reply
 

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