https://leetcode-cn.com/problems/serialize-and-deserialize-binary-tree/

    1. /**
    2. * Definition for a binary tree node.
    3. * class TreeNode {
    4. * val: number
    5. * left: TreeNode | null
    6. * right: TreeNode | null
    7. * constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
    8. * this.val = (val===undefined ? 0 : val)
    9. * this.left = (left===undefined ? null : left)
    10. * this.right = (right===undefined ? null : right)
    11. * }
    12. * }
    13. */
    14. /*
    15. * Encodes a tree to a single string.
    16. */
    17. function serialize(root: TreeNode | null): string {
    18. return rserialize(root, '');
    19. }
    20. /*
    21. * Decodes your encoded data to tree.
    22. */
    23. function deserialize(data: string): TreeNode | null {
    24. const arr = data.split(',');
    25. return buildTree(arr);
    26. }
    27. function rserialize(root: TreeNode | null, str: string) {
    28. if (root === null) {
    29. str+= 'None,';
    30. } else {
    31. str += root.val + ',';
    32. str = rserialize(root.left, str);
    33. str = rserialize(root.right, str);
    34. }
    35. return str;
    36. }
    37. function buildTree(list: string[]) {
    38. const rootVal = list.shift();
    39. if (rootVal === 'None') return null;
    40. const root = new TreeNode(parseInt(rootVal));
    41. root.left = buildTree(list);
    42. root.right = buildTree(list);
    43. return root;
    44. }

    渲染百万条结构简单的大数据时 怎么使用分片思想优化渲染