day82(2.10)——leetcode面试经典150

427. 建立四叉树

427. 建立四叉树

这个题目把我吓到了哈哈哈 如此之多

题目:

题解:

java 复制代码
/*
// Definition for a QuadTree node.
class Node {
    public boolean val;
    public boolean isLeaf;
    public Node topLeft;
    public Node topRight;
    public Node bottomLeft;
    public Node bottomRight;

    
    public Node() {
        this.val = false;
        this.isLeaf = false;
        this.topLeft = null;
        this.topRight = null;
        this.bottomLeft = null;
        this.bottomRight = null;
    }
    
    public Node(boolean val, boolean isLeaf) {
        this.val = val;
        this.isLeaf = isLeaf;
        this.topLeft = null;
        this.topRight = null;
        this.bottomLeft = null;
        this.bottomRight = null;
    }
    
    public Node(boolean val, boolean isLeaf, Node topLeft, Node topRight, Node bottomLeft, Node bottomRight) {
        this.val = val;
        this.isLeaf = isLeaf;
        this.topLeft = topLeft;
        this.topRight = topRight;
        this.bottomLeft = bottomLeft;
        this.bottomRight = bottomRight;
    }
}
*/

class Solution {
    public Node construct(int[][] grid) {
        int n = grid.length;
        return build(grid, 0, n-1, 0, n-1);
    }

    Node build(int[][] grid, int l1, int r1, int l2, int r2) {
        int flag = 0;
        int d = grid[l1][l2];
        for(int i=l1;i<=r1;i++) {
            for(int j=l2;j<=r2;j++) {
                if(grid[i][j]!=d) {
                    flag=1;
                    break;
                }
            }
            if(flag==1) {
                break;
            }
        }

        //如果该区域都相等,直接返回Node
        if(flag==0) {
            return new Node(d == 1, true);
        }

        int mid1 = (l1+r1)/2;
        int mid2 = (l2+r2)/2;

        Node tL = build(grid, l1, mid1, l2, mid2);
        Node tR = build(grid, l1, mid1, mid2+1, r2);
        Node bL = build(grid, mid1+1, r1, l2, mid2);
        Node bR = build(grid, mid1+1, r1, mid2+1, r2);
       
        return new Node(false, false, tL, tR, bL, bR);
    }
}
相关推荐
洛卡卡了10 小时前
我们在用 AI 写代码时,为什么建议要好好维护 AGENTS.md 呢?
面试·agent·claude
PBitW10 小时前
GPT训练我的第三天,明白了应该咋说满分回答!😕😕😕
前端·javascript·面试
自由路飞16 小时前
RAG 混合检索深挖:BM25 和向量分数为什么不能直接相加?
面试
未秃头的程序猿16 小时前
告别"if-else地狱"!Java 21模式匹配,代码优雅了10倍
java·后端·面试
阳光是sunny1 天前
Vue 项目怎么做用户行为全链路监控?轻量插件方案详解
前端·面试·架构
蝎子莱莱爱打怪1 天前
DSpark 讲透:DeepSeek 不换模型,硬把 V4 提速 85%,是怎么做到的?
人工智能·面试·程序员
程序员七平2 天前
面试官:你说你Vibe Coding手拿把掐,那 Claude Code 用户级、项目级、本地级配置怎么隔离?
面试
葫芦和十三2 天前
图解 MongoDB 17|大集合与工作集:数据超过内存怎么办
后端·mongodb·面试
葫芦和十三2 天前
图解 MongoDB 18|复制集拓扑:Primary、Secondary 和 Arbiter 的分工
后端·mongodb·面试
葫芦和十三2 天前
图解 MongoDB 15|journal 与持久化:写入怎么不丢,崩溃怎么恢复
后端·mongodb·面试