# Definition for a binary tree node.# class TreeNode(object):# def __init__(self, x):# self.val = x# self.left = None# self.right = NoneclassCodec:defserialize(self,root):"""Encodes a tree to a single string. :type root: TreeNode :rtype: str """ifrootisNone:return''res=[]defpreorder(root):ifrootisNone:res.append("#,")returnres.append(str(root.val)+",")preorder(root.left)preorder(root.right)preorder(root)return''.join(res)defdeserialize(self,data):"""Decodes your encoded data to tree. :type data: str :rtype: TreeNode """ifnotdata:returnNonevals=data.split(',')definner():first=vals.pop(0)iffirst=='#':returnNonereturnTreeNode(int(first),inner(),inner())returninner()# Your Codec object will be instantiated and called as such:# ser = Codec()# deser = Codec()# ans = deser.deserialize(ser.serialize(root))
/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */publicclassCodec{privatestaticfinalStringNULL="#";privatestaticfinalStringSEP=",";// Encodes a tree to a single string.publicStringserialize(TreeNoderoot){if(root==null){return"";}StringBuildersb=newStringBuilder();preorder(root,sb);returnsb.toString();}privatevoidpreorder(TreeNoderoot,StringBuildersb){if(root==null){sb.append(NULL+SEP);return;}sb.append(root.val+SEP);preorder(root.left,sb);preorder(root.right,sb);}// Decodes your encoded data to tree.publicTreeNodedeserialize(Stringdata){if(data==null||"".equals(data)){returnnull;}List<String>vals=newLinkedList<>();for(Stringx:data.split(SEP)){vals.add(x);}returndeserialize(vals);}privateTreeNodedeserialize(List<String>vals){Stringfirst=vals.remove(0);if(NULL.equals(first)){returnnull;}TreeNoderoot=newTreeNode(Integer.parseInt(first));root.left=deserialize(vals);root.right=deserialize(vals);returnroot;}}// Your Codec object will be instantiated and called as such:// Codec ser = new Codec();// Codec deser = new Codec();// TreeNode ans = deser.deserialize(ser.serialize(root));
/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */classCodec{public:// Encodes a tree to a single string.stringserialize(TreeNode*root){if(!root)return"";strings="";preorder(root,s);returns;}voidpreorder(TreeNode*root,string&s){if(!root)s+="# ";else{s+=to_string(root->val)+" ";preorder(root->left,s);preorder(root->right,s);}}// Decodes your encoded data to tree.TreeNode*deserialize(stringdata){if(data=="")returnnullptr;stringstreamss(data);returndeserialize(ss);}TreeNode*deserialize(stringstream&ss){stringfirst;ss>>first;if(first=="#")returnnullptr;TreeNode*root=newTreeNode(stoi(first));root->left=deserialize(ss);root->right=deserialize(ss);returnroot;}};// Your Codec object will be instantiated and called as such:// Codec ser, deser;// TreeNode* ans = deser.deserialize(ser.serialize(root));
/** * Definition for a binary tree node. * function TreeNode(val) { * this.val = val; * this.left = this.right = null; * } *//** * Encodes a tree to a single string. * * @param {TreeNode} root * @return {string} */varserialize=function(root){returnrserialize(root,'');};/** * Decodes your encoded data to tree. * * @param {string} data * @return {TreeNode} */vardeserialize=function(data){constdataArray=data.split(',');returnrdeserialize(dataArray);};constrserialize=(root,str)=>{if(root===null){str+='#,';}else{str+=root.val+''+',';str=rserialize(root.left,str);str=rserialize(root.right,str);}returnstr;};constrdeserialize=dataList=>{if(dataList[0]==='#'){dataList.shift();returnnull;}constroot=newTreeNode(parseInt(dataList[0]));dataList.shift();root.left=rdeserialize(dataList);root.right=rdeserialize(dataList);returnroot;};/** * Your functions will be called as such: * deserialize(serialize(root)); */