Python simple solution preorder


  • 1
    A
    
    class Codec:
    
        def serialize(self, root):
    
            if root: return str(root.val)+" "+ self.serialize(root.left) + " "+self.serialize(root.right) 
            else: return "#"
    
    
        def deserialize(self, data):
    
            vals = data.split(" ")
            self.i=0
            
            def helper():
                if vals[self.i]=="#": 
                    return None
                
                node = TreeNode(vals[self.i])
                self.i+=1
                node.left = helper()
                self.i+=1
                node.right = helper()
                return node
    
            return helper()
    
    

Log in to reply
 

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