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 ...
随机推荐
- [hdu1533]二分图最大权匹配 || 最小费用最大流
题意:给一个n*m的地图,'m'表示人,'H'表示房子,求所有人都回到房子所走的距离之和的最小值(距离为曼哈顿距离). 思路:比较明显的二分图最大权匹配模型,将每个人向房子连一条边,边权为曼哈顿距离的 ...
- 解决Eclipse添加新server时无法选择Tomcat7.0
新添加tomcat时 出现如下图情况: 解决方法:这时打开工作空间目录下的.metadata\.plugins\org.eclipse.core.runtime\.settings文件夹,删除org. ...
- mysql 库表整体相关查询
select table_schema,table_name from information_schema.columns where column_name = '字段名'; 查询某张表有几条记录 ...
- SpringBoot 整合SpringBatch实际项目改造
SpringBoot整合SpringBatch项目,已将代码开源至github,访问地址:https://github.com/cmlbeliever/SpringBatch 欢迎star or fo ...
- php5.2安装memcached 扩展
需要注意版本号,好坑. libmemcached release 1.0.16 - installed from sourcephp-memcached release 2.1.0 - install ...
- Django之ORM外部python脚本使用
python脚本使用django的ROM 如果你想通过自己创建的python文件在django项目中使用django的models,那么就需要调用django的环境: 在总的项目文件夹创建的py文件: ...
- rasdaman介绍及安装
一.分布式介绍 Rasdaman中的主节点称为Rasdaman的主机,它充当中央Rasdaman请求分派器并且控制所有服务器进程.Rasdaman管理器接收客户机请求并将这些请求分配给服务器进程.服务 ...
- DFS序--一般都要转化为顶点到每个点
There is a rooted tree with n nodes, number from 1-n. Root’s number is 1.Each node has a value ai. I ...
- LightOJ1236
题目大意: 给你一个 n,请你找出共有多少对(i,j)满足 lcm(i,j) = n (i<=j) . 解题思路: 我们利用算术基本定理将 n,i,j 进行分解: n = P1a1 * P2a2 ...
- Longest Mountain in Array 数组中的最长山脉
我们把数组 A 中符合下列属性的任意连续子数组 B 称为 “山脉”: B.length >= 3 存在 0 < i < B.length - 1 使得 B[0] < B[1] ...