1

The question that I am asking here is based on www.dailycodingproblem.com

question 3: "Given the root to a binary tree, implement serialize(root), which serializes the tree into a string, and deserialize(s), which deserializes the string back into the tree."

The code below is one of the solutions that I found and the question I want to ask is:-

For the deserialize function in line 42, when I pass an argument such as

def deserializer(node): which is followed by return deserializer(node), the output says "node not defined error" if I input any numbers eg: 1,3,2.

However, it works when I leave out the argument part empty that is def deserializer() followed by return deserializer()?

Your help would be much appreciated!

#must create a constructor every time we create a class eg: def __init__(self)
class Node:
    def __init__(self, v):
        self.left = None #none = empty state
        self.right = None
        self.value = v

class Tree:
    def __init__(self):
        self.root = None

    def addNode(self, node, v1):
        if node == None:
            self.root = Node(v1) 
            #argument in Node need not have to be v
        else:
            if v1 < node.value: 
                #if 2nd value less than 1st value for example?
                if not node.left: 
                    node.left = Node(v1) #will not update value

                else:
                    #if it is node.left,update value
                    self.addNode(node.left, v1) 

            else:
                if not node.right:
                    node.right = Node(v1)
                else:
                    self.addNode(node.right, v1)


def deserialize(s):
    values = iter(s.split(','))
    def deserializer(): #why putting def deserializer(node) gives me "node undefined error"?)
        val = next(values)
        if val == '?':
            return None
        else:
            node = Node(int(val))
            node.left = deserializer()
            node.right = deserializer()
            return node
    return deserializer() #why putting return deserializer(node) gives me "node undefined error"?)

if __name__ == '__main__':
    # Read input, numbers separated by commas
    numbers = [int(n) for n in input().split(',')]
    theTree = Tree()
    for number in numbers:
        theTree.addNode(theTree.root, number)
    s2 = serialize(deserialize(s1))
    print(s2)
user78910
  • 349
  • 2
  • 12
john
  • 191
  • 8
  • 1
    The error is absolutely correct because of the nested function deserializer doesn't have any argument defined in the code. The function is not deserializer(node) but only deserializer() without any argument inside the brackets. Just don't give the node argument and this code will work fine. – user78910 Apr 27 '20 at 13:31
  • @user78910 but is it possible to have an argument in it? Something relating to deserializer(node) in this case ? Also how do i know when should i include argument in a function? – john Apr 27 '20 at 17:40
  • 1
    yes you can definitely define an argument into child function i.e. deserializer(node) is okay. Just remember deserializer function is used many times in your code so update it everywhere it is getting used. It would become a bit complicated but you may try. Also, check [this](https://stackoverflow.com/questions/2005956/how-do-nested-functions-work-in-python#2005992) for function call. It's quite an abstract question, John. However, python is quite flexible but you have to be aware of scope of functions. I would suggest that check for types of arguments in python tutorials. :) – user78910 May 06 '20 at 13:41

0 Answers0