<s> """<STR_LIT>""" <EOL> from collections import deque <EOL> __author__ = '<STR_LIT>' <EOL> class TreeNode ( object ) : <EOL> def __init__ ( self , x ) : <EOL> self . val = x <EOL> self . left = None <EOL> self . right = None <EOL> class Codec : <EOL> def serialize ( self , root ) : <EOL> """<STR_LIT>""" <EOL> if not root : <EOL> return "<STR_LIT:null>" <EOL> ret = [ ] <EOL> q = [ ] <EOL> q . append ( root ) <EOL> ret . append ( str ( root . val ) ) <EOL> while q : <EOL> l = len ( q ) <EOL> for i in xrange ( l ) : <EOL> cur = q [ i ] <EOL> if cur . left : q . append ( cur . left ) <EOL> ret . append ( self . encode ( cur . left ) ) <EOL> if cur . right : q . append ( cur . right ) <EOL> ret . append ( self . encode ( cur . right ) ) <EOL> q = q [ l : ] <EOL> return "<STR_LIT:U+002C>" . join ( ret ) <EOL> def deserialize ( self , data ) : <EOL> """<STR_LIT>""" <EOL> lst = data . split ( "<STR_LIT:U+002C>" ) <EOL> root = self . decode ( lst [ <NUM_LIT:0> ] ) <EOL> q = deque ( [ root ] ) <EOL> i = <NUM_LIT:1> <EOL> while q : <EOL> cur = q . popleft ( ) <EOL> if i < len ( lst ) : <EOL> cur . left = self . decode ( lst [ i ] ) <EOL> i += <NUM_LIT:1> <EOL> if cur . left : q . append ( cur . left ) <EOL> if i < len ( lst ) : <EOL> cur . right = self . decode ( lst [ i ] ) <EOL> i += <NUM_LIT:1> <EOL> if cur . right : q . append ( cur . right ) <EOL> return root <EOL> def decode ( self , s ) : <EOL> if s == "<STR_LIT:null>" : <EOL> return None <EOL> else : <EOL> return TreeNode ( int ( s ) ) <EOL> def encode ( self , node ) : <EOL> if not node : <EOL> return "<STR_LIT:null>" <EOL> else : <EOL> return str ( node . val ) </s>
