博客记录-day164-力扣

103 阅读2分钟

一、力扣

! 1、删除二叉搜索树中的节点

450. 删除二叉搜索树中的节点

image.png

class Solution {
    public TreeNode deleteNode(TreeNode root, int key) {
        // 如果当前节点为空,直接返回null(递归终止条件)
        if (root == null) return null;

        // 根据BST性质,确定搜索方向
        if (root.val < key) {
            // 待删除节点在右子树,递归处理右子树,并更新当前节点的右子树
            root.right = deleteNode(root.right, key);
        } else if (root.val > key) {
            // 待删除节点在左子树,递归处理左子树,并更新当前节点的左子树
            root.left = deleteNode(root.left, key);
        } else {
            // 找到待删除节点,处理三种情况
            
            // 情况1:左子树为空,直接返回右子树作为新的子节点
            if (root.left == null) return root.right;
            // 情况2:右子树为空,直接返回左子树作为新的子节点
            if (root.right == null) return root.left;

            // 情况3:左右子树均存在
            // 找到右子树中的最小节点(即右子树最左侧节点)
            TreeNode ans = root.right;
            while (ans.left != null) {
                ans = ans.left;
            }
            // 将最小节点的值复制到当前节点,保持BST结构
            root.val = ans.val;
            // 删除右子树中的最小节点(该节点无左子树,递归调用会进入情况1或2)
            root.right = deleteNode(root.right, ans.val);
        }
        // 返回处理后的当前节点(可能已被替换或子树调整)
        return root;
    }
}

2、每日温度

739. 每日温度

image.png

class Solution {
    public int[] dailyTemperatures(int[] temperatures) {
        int n = temperatures.length;
        ArrayDeque<Integer> queue = new ArrayDeque<>();
        int[] res = new int[n];
        for (int i = n - 1; i >= 0; i--) {
            while (!queue.isEmpty() && temperatures[queue.peek()] <= temperatures[i]) {
                queue.pollFirst();
            }
            if (!queue.isEmpty())
                res[i] = queue.peek() - i;
            queue.offerFirst(i);
        }
        return res;
    }
}

3、接雨水

42. 接雨水

image.png

class Solution {
    public int trap(int[] height) {
        int n = height.length;
        int[] left = new int[n];
        left[0] = height[0];
        for (int i = 1; i < n; i++) {
            left[i] = Math.max(left[i - 1], height[i]);
        }
        int[] right = new int[n];
        right[n - 1] = height[n - 1];
        for (int i = n - 2; i >= 0; i--) {
            right[i] = Math.max(right[i + 1], height[i]);
        }
        int res = 0;
        for (int i = 0; i < n; i++) {
            res += Math.min(left[i], right[i]) - height[i];
        }
        return res;
    }
}

4、柱状图中最大的矩形

84. 柱状图中最大的矩形

image.png

class Solution {
    public int largestRectangleArea(int[] heights) {
        int n = heights.length;
        int[] left = new int[n];
        ArrayDeque<Integer> stack = new ArrayDeque<>();
        for (int i = 0; i < n; i++) {
            while (!stack.isEmpty() && heights[stack.peek()] >= heights[i]) {
                stack.pop();
            }
            if (!stack.isEmpty()) {
                left[i] = stack.peek();
            } else {
                left[i] = -1;
            }
            stack.push(i);
        }
        int[] right = new int[n];
        stack = new ArrayDeque<>();
        for (int i = n - 1; i >= 0; i--) {
            while (!stack.isEmpty() && heights[stack.peek()] >= heights[i]) {
                stack.pop();
            }
            if (!stack.isEmpty()) {
                right[i] = stack.peek();
            } else {
                right[i] = n;
            }
            stack.push(i);
        }
        int res = 0;
        for (int i = 0; i < n; i++) {
            res = Math.max(res, (right[i] - left[i] - 1) * heights[i]);
        }
        return res;
    }
}