You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
/** * 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){letstr='';letreSerialize=(root)=>{if(root===null){str+='#,'}else{str+=root.val+',';root.left=reSerialize(root.left);root.right=reSerialize(root.right);}returnstr;}returnreSerialize(root);};/** * Decodes your encoded data to tree. * * @param {string} data * @return {TreeNode} */vardeserialize=function(data){letarr=data.split(',')console.log(arr);constreDeserialize=(arr)=>{if(arr[0]==='#'){arr.shift();returnnull;}constroot=newTreeNode(parseInt(arr[0]));arr.shift();root.left=reDeserialize(arr);root.right=reDeserialize(arr);returnroot;}returnreDeserialize(arr);};/** * Your functions will be called as such: * deserialize(serialize(root)); */
The text was updated successfully, but these errors were encountered:
The text was updated successfully, but these errors were encountered: