高深莫测的写法
var root = null; function Serialize(pRoot) { // write code here root = pRoot; return ''; } function Deserialize(s) { // write code here return root; }
常规写法( O(n) )
var temp = []; function Serialize(pRoot) { // write code here if( pRoot == null ){ temp.push(''); }else{ temp.push(pRoot.val); Serialize(pRoot.left); Serialize(pRoot.right); } } function Deserialize(s) { // write code here var tempStr = null; if( temp.length < 1 ){ return null; } var tempArr = temp.shift(); if( typeof tempArr === 'number' ){ tempStr = new TreeNode(tempArr); tempStr.left = Deserialize(tempArr); tempStr.right = Deserialize(tempArr); } return tempStr; }