https://leetcode-cn.com/problems/serialize-and-deserialize-binary-tree/
/**
* Definition for a binary tree node.
* class TreeNode {
* val: number
* left: TreeNode | null
* right: TreeNode | null
* constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
* this.val = (val===undefined ? 0 : val)
* this.left = (left===undefined ? null : left)
* this.right = (right===undefined ? null : right)
* }
* }
*/
/*
* Encodes a tree to a single string.
*/
function serialize(root: TreeNode | null): string {
return rserialize(root, '');
}
/*
* Decodes your encoded data to tree.
*/
function deserialize(data: string): TreeNode | null {
const arr = data.split(',');
return buildTree(arr);
}
function rserialize(root: TreeNode | null, str: string) {
if (root === null) {
str+= 'None,';
} else {
str += root.val + ',';
str = rserialize(root.left, str);
str = rserialize(root.right, str);
}
return str;
}
function buildTree(list: string[]) {
const rootVal = list.shift();
if (rootVal === 'None') return null;
const root = new TreeNode(parseInt(rootVal));
root.left = buildTree(list);
root.right = buildTree(list);
return root;
}
渲染百万条结构简单的大数据时 怎么使用分片思想优化渲染