427. 建立四叉树

我们想要使用一棵四叉树来储存一个 N x N 的布尔值网络。网络中每一格的值只会是真或假。树的根结点代表整个网络。对于每个结点, 它将被分等成四个孩子结点直到这个区域内的值都是相同的.

每个结点还有另外两个布尔变量: isLeaf 和 val。isLeaf 当这个节点是一个叶子结点时为真。val 变量储存叶子结点所代表的区域的值。

你的任务是使用一个四叉树表示给定的网络。下面的例子将有助于你理解这个问题:

给定下面这个8 x 8 网络,我们将这样建立一个对应的四叉树:

由上文的定义,它能被这样分割:

对应的四叉树应该像下面这样,每个结点由一对 (isLeaf, val) 所代表.

对于非叶子结点,val 可以是任意的,所以使用 * 代替。

提示:

N 将小于 1000 且确保是 2 的整次幂。

其实这个题有一些像前段时间看到得棋盘覆盖

/*
// 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) {
return construct(grid, 0, grid.length - 1, 0, grid.length - 1);
} private Node construct(int[][] grid, int top, int bottom, int left, int right) {
for (int i = top; i <= bottom; i++) {
for (int j = left; j <= right; j++) {
if (grid[i][j] != grid[top][left]) {
Node node = new Node(false, false);
int mid1 = top + ((bottom - top) >> 1), mid2 = left + ((right - left) >> 1);
node.topLeft = construct(grid, top, mid1, left, mid2);
node.topRight = construct(grid, top, mid1, mid2 + 1, right);
node.bottomLeft = construct(grid, mid1 + 1, bottom, left, mid2);
node.bottomRight = construct(grid, mid1 + 1, bottom, mid2 + 1, right);
return node;
}
}
}
return new Node(grid[top][left] == 1, true);
} }

Java实现 LeetCode 427 建立四叉树的更多相关文章

  1. Leetcode 427.建立四叉树

    建立四叉树 我们想要使用一棵四叉树来储存一个 N x N 的布尔值网络.网络中每一格的值只会是真或假.树的根结点代表整个网络.对于每个结点, 它将被分等成四个孩子结点直到这个区域内的值都是相同的. 每 ...

  2. Java for LeetCode 126 Word Ladder II 【HARD】

    Given two words (start and end), and a dictionary, find all shortest transformation sequence(s) from ...

  3. Java for LeetCode 216 Combination Sum III

    Find all possible combinations of k numbers that add up to a number n, given that only numbers from ...

  4. Java for LeetCode 214 Shortest Palindrome

    Given a string S, you are allowed to convert it to a palindrome by adding characters in front of it. ...

  5. Java for LeetCode 212 Word Search II

    Given a 2D board and a list of words from the dictionary, find all words in the board. Each word mus ...

  6. Java for LeetCode 211 Add and Search Word - Data structure design

    Design a data structure that supports the following two operations: void addWord(word)bool search(wo ...

  7. Java for LeetCode 210 Course Schedule II

    There are a total of n courses you have to take, labeled from 0 to n - 1. Some courses may have prer ...

  8. Java for LeetCode 200 Number of Islands

    Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. An island is surro ...

  9. Java for LeetCode 188 Best Time to Buy and Sell Stock IV【HARD】

    Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...

随机推荐

  1. 单向环形链表解决约瑟夫环(Josephus)问题

    一.约瑟夫环问题 Josephu 问题为:设编号为1,2,- n的n个人围坐一圈,约定编号为k(1<=k<=n)的人从1开始报数,数到m 的那个人出列,它的下一位又从1开始报数,数到m的那 ...

  2. u-boot spl 学习总结

    什么是SPL? SPL(secondary program loader)是一个十分小的bin文件,它是用来引导主u-boot文件.对于一些SRAM很小的SOC,无法一次性加载ROM中的bootloa ...

  3. [acdream_oj1732]求1到n的最小公倍数(n<=1e8)

    题意:如标题 思路:如果n在10^6以内则可以用o(nlogn)的暴力,题目给定的是n<=1e8,暴力显然是不行的,考虑到1到n的最小公倍数可以写成2^p1*3^p2*5^p3*...这种素数的 ...

  4. 豹子安全-注入工具-显错注入-asp_POST_显错_SQLServer_GetWebShell

    豹子安全-注入工具-显错注入-SQL Server-上传WebShell 请看下列视频 . 该视频在停留10秒钟后开始. 或点击此处查看:豹子安全-注入工具-asp_POST_显错_SQLServer ...

  5. 散列表PTA判断

    1-1 在散列表中,所谓同义词就是具有相同散列地址的两个元素. (1分) T         F 作者 DS课程组 单位 浙江大学   1-2 采用平方探测冲突解决策略(h​i​​(k)=(H(k)+ ...

  6. Node.js服务器创建和使用

    1.使用zlib模块对服务器端响应压缩 //1.1引入zlib模块 const zlib=require('zlib'); //1.2 设置内容的压缩形式 'Content-Encoding': 'g ...

  7. nginx配置之代理功能

    nginx代理功能 至少需要两台主机: 代理机配置----如下: yum安装的/etc/nginx/nginx.conf location / { #服务机的ip端口 proxy_pass http: ...

  8. vue打包时semver.js版本报错

    如图,报错semver.js版本不正确,解决思路是在node_modules/semver.js内,忽略版本检查 ''' // if ANY of the sets match ALL of its ...

  9. Spring 自动装配 byType

    自动装配 byType,这种模式由属性类型指定自动装配. Spring 容器看作 beans,在 XML 配置文件中 beans 的 autowire 属性设置为 byType.然后,如果它的 typ ...

  10. 【MySQL】大白话讲讲主从架构的几种搭配方式详解

    话不多,直接上图: 主要来详细讲讲各个搭配 1>一主一从(成本最低): 并不是用来提高程序性能的,主要是用来做数据的热备(即如果master节点挂掉的话,slave节点能充当master节点), ...