1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| var serialize = function (root) { if (!root) return 'null'; return root.val + ',' + serialize(root.left) + ',' + serialize(root.right); }; var deserialize = function (data) { const nodes = data.split(','); let idx = 0; function build() { if (nodes[idx] === 'null') { idx++; return null; } const node = new TreeNode(+nodes[idx++]); node.left = build(); node.right = build(); return node; } return build(); };
|