https://leetcode-cn.com/problems/maximum-depth-of-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)
* }
* }
*/
function maxDepth(root: TreeNode | null): number {
if (!root) return null;
const left = maxDepth(root.left);
const right = maxDepth(root.right);
return 1 + Math.max(left, right);
};
https://bigfrontend.dev/zh/problem/immerjs
type ProduceFunc = <T>(base: T, receipe: (draft: T) => any) => void
const produce: ProduceFunc = (base, recipe) => {
const copy = deepCopy(base);
recipe(copy);
if (compare(base, copy)) {
return base
}
return copy
}
function deepCopy(obj: any) {
let output: any = Array.isArray(obj) ? [] : {};
if (obj === null || typeof obj !== 'object') {
return obj;
}
for (let key in obj) {
output[key] = deepCopy(obj[key])
}
return output;
}
function compare(origin: any, modified: any) {
if (typeof origin !== typeof modified) return false
if (typeof origin !== 'object') {
return origin === modified;
}
let isEqual = true
for (let key in origin) {
if (compare(origin[key], modified[key])) {
modified[key] = origin[key]
} else {
isEqual = false
}
}
return Object.keys(origin).length === Object.keys(modified).length && isEqual
}