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);
    }
}
相关推荐
nice_lcj52014 小时前
【从零理解计算机网络 · 13】DNS 解析全流程:域名怎么变成 IP,慢在哪
网络·计算机网络·面试·dns·域名解析·cname·ttl
nice_lcj52016 小时前
【从零理解计算机网络 · 12】登录态的载体:Cookie、Session、Token 在网络层怎么走
后端·计算机网络·面试·jwt·token·cookie·session
ShineWinsu17 小时前
对于Qt:QMessageBox的解析
开发语言·c++·qt·面试·客户端·qmessagebox·消息对话框
嵌入式 Linux手记1 天前
嵌入式面试都考的置位清位:位运算四件套
linux·c语言·驱动开发·面试·职场和发展·嵌入式
nice_lcj5201 天前
【从零理解计算机网络 · 15】完整串联:一次请求的全旅程,以及那道经典面试题
网络·计算机网络·http·面试·tcp·总结·网络排查
Pniubi1 天前
力扣2 . 两数相加
算法·leetcode·职场和发展
im_AMBER2 天前
CDC 对账:数据同步之后,如何验证源库与目标库一致
后端·mysql·面试
Pniubi3 天前
力扣21合并两个有序链表
算法·leetcode·职场和发展
宋艳_AI应用开发3 天前
Agent 工程:慢的不一定是模型,答案对了也可能算失败
面试
斯维赤3 天前
三级缓存为什么是三级?二级缓存不够吗
前端·后端·面试