https://leetcode-cn.com/problems/maximum-depth-of-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. function maxDepth(root: TreeNode | null): number {
    15. if (!root) return null;
    16. const left = maxDepth(root.left);
    17. const right = maxDepth(root.right);
    18. return 1 + Math.max(left, right);
    19. };

    https://bigfrontend.dev/zh/problem/immerjs

    1. type ProduceFunc = <T>(base: T, receipe: (draft: T) => any) => void
    2. const produce: ProduceFunc = (base, recipe) => {
    3. const copy = deepCopy(base);
    4. recipe(copy);
    5. if (compare(base, copy)) {
    6. return base
    7. }
    8. return copy
    9. }
    10. function deepCopy(obj: any) {
    11. let output: any = Array.isArray(obj) ? [] : {};
    12. if (obj === null || typeof obj !== 'object') {
    13. return obj;
    14. }
    15. for (let key in obj) {
    16. output[key] = deepCopy(obj[key])
    17. }
    18. return output;
    19. }
    20. function compare(origin: any, modified: any) {
    21. if (typeof origin !== typeof modified) return false
    22. if (typeof origin !== 'object') {
    23. return origin === modified;
    24. }
    25. let isEqual = true
    26. for (let key in origin) {
    27. if (compare(origin[key], modified[key])) {
    28. modified[key] = origin[key]
    29. } else {
    30. isEqual = false
    31. }
    32. }
    33. return Object.keys(origin).length === Object.keys(modified).length && isEqual
    34. }