Java实现 LeetCode 427 建立四叉树
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 建立四叉树的更多相关文章
- Leetcode 427.建立四叉树
建立四叉树 我们想要使用一棵四叉树来储存一个 N x N 的布尔值网络.网络中每一格的值只会是真或假.树的根结点代表整个网络.对于每个结点, 它将被分等成四个孩子结点直到这个区域内的值都是相同的. 每 ...
- Java for LeetCode 126 Word Ladder II 【HARD】
Given two words (start and end), and a dictionary, find all shortest transformation sequence(s) from ...
- 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 ...
- 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. ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
随机推荐
- 解决MySQL 8.0数据库出现乱码的问题
1.在MySQL 8.0的安装目录下创建一个my.ini文件(保存为utf8格式),然后写入以下内容: [mysql] # 设置mysql客户端默认编码 default-character-set=u ...
- 明解JAVA 第三章答案
练习3-1 package candle1220; import java.util.Scanner; public class Nightwatch { public static void mai ...
- CodeForces - 1209D Cow and Snacks 并查集
CodeForces - 1209D 题意 现在n种点心,每种点心只有一份,有k位客人,每位客人有两种想要吃的点心,你可以安排他们进场的顺序,每位客人会吃掉所有他想要吃的,并且还没被吃掉的点心.如果客 ...
- FPGA学习心得汇总(手中写代码,心中有电路)
http://bbs.ednchina.com/BLOG_ARTICLE_2111172.HTM 任何的时序逻辑都可以转换为组合逻辑+D触发器来完成. FPGA内部主要三块:可编程的逻辑单元.可编程的 ...
- 在ef core中使用postgres数据库的全文检索功能实战之中文支持
前言 有关通用的postgres数据库全文检索在ef core中的使用方法,参见我的上一篇文章. 本文实践了zhparser中文插件进行全文检索. 准备工作 安装插件,最方便的方法是直接使用安装好插件 ...
- Tomcat session的实现:线程安全与管理
本文所说的session是单机版本的session, 事实上在当前的互联网实践中已经不太存在这种定义了.我们主要讨论的是其安全共享的实现,只从理论上来讨论,不必太过在意实用性问题. 1. sessio ...
- C:习题1
C 语言具有哪些主要特点? 1.兼高级语言和汇编语言优点的语言 2.一种结构化程序设计语言 3.语言数据类型丰富 4.具有种类丰富的运算符 5.有预处理功能 C 语言的主要用途是什么? 1.作为一种系 ...
- 异步http接口调用库:httpx
谈到http接口调用,Requests大家并不陌生,例如,robotframework-requests.HttpRunner等HTTP接口测试库/框架都是基于它开发.这里将介绍另一款http接口测试 ...
- python3.x 基础三:装饰器
装饰器:本质是函数,用于装饰其他函数,在不改变其他函数的调用和代码的前提下,增加新功能 原则: 1.不能修改被装饰函数的源代码 2.不能修改被装饰函数的调用方式 3.装饰函数对于被装饰函数透明 参考如 ...
- oracle分析函数Rank, Dense_rank, row_number
http://www.cnblogs.com/wuyisky/archive/2010/02/24/oracle_rank.html 目录=============================== ...