算法学习记录(六十)

67 阅读1分钟

问:

  1. 剑指 Offer 26. 树的子结构
  2. 剑指 Offer 27. 二叉树的镜像
  3. 剑指 Offer 28. 对称的二叉树
  4. 剑指 Offer 29. 顺时针打印矩阵
  5. 剑指 Offer 30. 包含min函数的栈 解:
const isSubStructure = function(head1, head2) {
    if (!head1 ||!head2) return false
    function getRes(node1) {
        if (!node1) return false
        return isMatch(node1, head2) || getRes(node1.left) || getRes(node1.right)
    }
    function isMatch(node1, node2) {
        if (!node2) return true
        if (!node1) return false
        return node1.val === node2.val && isMatch(node1.left, node2.left) && isMatch(node1.right, node2.right)
    }
    return getRes(head1, head2)
};
const mirrorTree = function(head) {
     function getRes(node) {
        if (!node) return null
        const curNode = new TreeNode(node.val)
        curNode.right = getRes(node.left)
        curNode.left = getRes(node.right)
        return curNode
    }
    return getRes(head)
};
const isSymmetric = function(head) {
     function getRes(node, node2) {
        if (!node && !node2) return true
        if (!node || !node2) return false
        return node.val === node2.val && getRes(node.left, node2.right) && getRes(node.right, node2.left)
    }
    return getRes(head, head)
};
const spiralOrder = function(matrix) {
    if (!matrix.length) return []
    let leftUpX = 0
    let leftUpY = 0
    let rightDownX = matrix.length - 1
    let rightDownY = matrix[0].length - 1
    const res = []
    while (leftUpX <= rightDownX && leftUpY <= rightDownY) {
        res.push(...print(leftUpX, leftUpY, rightDownX, rightDownY))
        leftUpX++
        leftUpY++
        rightDownX--
        rightDownY--
    }
    return res
    function print(x,y,a,b) {
        const res = []
        let start = y
        if (x === a) {
            for (let i = y; i <= b; i++) {
                res.push(matrix[x][i])
            }
        } else if (y === b) {
            for (let i = x; i <= a; i++) {
                res.push(matrix[i][b])
            }
        } else {
            while (start < b) {
                res.push(matrix[x][start])
                start++
            }
            start = x
            while (start < a) {
                res.push(matrix[start][b])
                start++
            }
            start = b
            while (start > y) {
                res.push(matrix[a][start])
                start--
            }
            start = a
            while (start > x) {
                res.push(matrix[start][y])
                start--
            }
        }
        return res
    }
};
class MinStack {
    public stack1: number[]
    public stack2: number[]
    constructor() {
        this.stack1 = []
        this.stack2 = []
    }

    push(x: number): void {
        this.stack1.push(x)
        const curMin = this.stack2[this.stack2.length - 1] ?? Infinity
        this.stack2.push(Math.min(x, curMin))
    }

    pop(): void {
        this.stack1.pop()
        this.stack2.pop()
    }

    top(): number {
        return this.stack1[this.stack1.length - 1]
    }

    min(): number {
        return this.stack2[this.stack2.length - 1]
    }
}